api_error_parser_plus 0.0.1
api_error_parser_plus: ^0.0.1 copied to clipboard
A library for parsing responses from api and converting error codes into messages for the user.
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 messagedefaultErrorMessage: E - message of unknown errors
Api parser description:
parse(ApiParserResponse<T> response)- returnsApiParserResponsein the states: success , empty or errorgetParserResponse(ApiResponse<T> response)- parses the server response object and returns the processed resultgetErrors(List<ErrorMessage> errors)- returns a list of processed errorsgetMessageFromCode(String errorCode)- returns the message associated with this error codegetMessage(ErrorMessage errorMessage)- returns the processed errorgetFirstMessage(List<ErrorMessage> errors)- returns the first processed error from the listgetFieldMessageFromCode(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;
}
}