api_error_parser_plus 0.1.0 copy "api_error_parser_plus: ^0.1.0" to clipboard
api_error_parser_plus: ^0.1.0 copied to clipboard

A robust and type-safe library for parsing API responses and converting error codes into user-friendly messages with comprehensive error handling and validation.

example/example.md

How it works #

The library provides ready-made interfaces for server responses to which the object passed to the parmer must correspond.

To initialize the ErrorParser, you must pass to the constructor: errorMessages:

  • Map<String, E> - the key is the error code and the value of the displayed message
  • defaultErrorMessage: E - message of unknown errors

Api parser description:

  • parse(ApiParserResponse<T> response) - returns ApiParserResponse in the states: success , empty or error
  • getParserResponse(ApiResponse<T> response) - parses the server response object and returns the processed result
  • getErrors(List<ErrorMessage> errors) - returns a list of processed errors
  • getMessageFromCode(String errorCode) - returns the message associated with this error code
  • getMessage(ErrorMessage errorMessage) - returns the processed error
  • getFirstMessage(List<ErrorMessage> errors) - returns the first processed error from the list
  • getFieldMessageFromCode(String field, String errorCode) - returns the first processed error from the list

Dart #

final apiParser = ApiParser({
                  CODE.ERROR_CODE: Message.ERROR_MESSAGE,
                }, Message.DEFAULT);
               
final ParserResponse<UserEntity> parserResponse = apiParser.getParserResponse(serverResponse);
                             
final ApiParserResponse<UserEntity> apiParserResponse = apiParser.parse(serverResponse); 

switch (apiParserResponse.runtimeType) {
    case  ApiParserEmptyResponse: {
      //do something
      break;
    }
    case  ApiParserErrorResponse: {
      //do something
      break;
    }
    case  ApiParserSuccessResponse: {
      //do something
      break;
    }
}
                            
3
likes
0
points
21
downloads

Publisher

unverified uploader

Weekly Downloads

A robust and type-safe library for parsing API responses and converting error codes into user-friendly messages with comprehensive error handling and validation.

Homepage

License

unknown (license)

Dependencies

flutter

More

Packages that depend on api_error_parser_plus