private void checkResponseCode ( String message ) throws ResponseException { if ( this . responseCode != 200 && this . responseCode != 204 ) { HashMap < Integer , String > responseCodes = new HashMap < Integer , String > ( ) { { put ( 206 , "PartialContent" ) ; put ( 400 , "Bad request" ) ; put ( 401 , "Unauthorized" ) ; put ( 403 , "Prohibition to use the method" ) ; put ( 404 , "Not found" ) ; put ( 405 , "Method not allowed" ) ; put ( 413 , "Request entity too large" ) ; put ( 422 , "Unprocessable entity" ) ; put ( 500 , "Internal server error" ) ; put ( 501 , "Not implemented" ) ; } } ; ResponseException responseException = new ResponseException ( message ) ; responseException . setResponseHttpCode ( this . responseCode ) ; if ( responseCodes . containsKey ( this . responseCode ) ) { responseException . setResponseHttpDescription ( responseCodes . get ( this . responseCode ) ) ; } else { responseException . setResponseHttpDescription ( "Unknown response error" ) ; } if ( message != null ) { JsonObject error = new JsonParser ( ) . parse ( message ) . getAsJsonObject ( ) ; for ( Entry < String , JsonElement > entry : error . entrySet ( ) ) { switch ( entry . getKey ( ) . toLowerCase ( ) ) { case "message" : responseException . setApiMessage ( entry . getValue ( ) . getAsString ( ) ) ; break ; case "type" : responseException . setType ( entry . getValue ( ) . getAsString ( ) ) ; break ; case "id" : responseException . setId ( entry . getValue ( ) . getAsString ( ) ) ; break ; case "date" : responseException . setDate ( ( int ) entry . getValue ( ) . getAsDouble ( ) ) ; break ; case "errors" : if ( entry . getValue ( ) == null ) break ; if ( entry . getValue ( ) . isJsonNull ( ) ) break ; for ( Entry < String , JsonElement > errorEntry : entry . getValue ( ) . getAsJsonObject ( ) . entrySet ( ) ) { if ( ! responseException . getErrors ( ) . containsKey ( errorEntry . getKey ( ) ) ) responseException . getErrors ( ) . put ( errorEntry . getKey ( ) , errorEntry . getValue ( ) . getAsString ( ) ) ; else { String description = responseException . getErrors ( ) . get ( errorEntry . getKey ( ) ) ; description = " | " + errorEntry . getValue ( ) . getAsString ( ) ; responseException . getErrors ( ) . put ( errorEntry . getKey ( ) , description ) ; } } break ; } } } throw responseException ; } }
Checks the HTTP response code and if it s neither 200 nor 204 throws a ResponseException .