private static String handleRichError(final Response response, final String body) {
    if (!response.getHeaders().containsKey(Headers.CONTENT_TYPE)
        || !response.getHeaders().get(Headers.CONTENT_TYPE).equals(ContentTypes.APPLICATION_JSON)) {
      return body;
    }

    final Document doc;
    try {
      doc = BsonUtils.parseValue(body, Document.class);
    } catch (Exception e) {
      return body;
    }

    if (!doc.containsKey(Fields.ERROR)) {
      return body;
    }
    final String errorMsg = doc.getString(Fields.ERROR);
    if (!doc.containsKey(Fields.ERROR_CODE)) {
      return errorMsg;
    }

    final String errorCode = doc.getString(Fields.ERROR_CODE);
    throw new StitchServiceException(errorMsg, StitchServiceErrorCode.fromCodeName(errorCode));
  }
private static String handleRichError ( final Response response , final String body ) { if ( ! response . getHeaders ( ) . containsKey ( Headers . CONTENT_TYPE ) || ! response . getHeaders ( ) . get ( Headers . CONTENT_TYPE ) . equals ( ContentTypes . APPLICATION_JSON ) ) { return body ; } final Document doc ; try { doc = BsonUtils . parseValue ( body , Document . class ) ; } catch ( Exception e ) { return body ; } if ( ! doc . containsKey ( Fields . ERROR ) ) { return body ; } final String errorMsg = doc . getString ( Fields . ERROR ) ; if ( ! doc . containsKey ( Fields . ERROR_CODE ) ) { return errorMsg ; } final String errorCode = doc . getString ( Fields . ERROR_CODE ) ; throw new StitchServiceException ( errorMsg , StitchServiceErrorCode . fromCodeName ( errorCode ) ) ; }
Private helper method which decodes the Stitch error from the body of an HTTP `Response`
object. If the error is successfully decoded, this function will throw the error for the end
user to eventually consume. If the error cannot be decoded, this is likely not an error from
the Stitch server, and this function will return an error message that the calling function
should use as the message of a StitchServiceException with an unknown code.