api_error_parser 0.0.4

  • Readme
  • Changelog
  • Installing
  • 69

API Parser #

License: MIT

A library for parsing responses from api and converting error codes into messages for the user.

API response description #

It is assumed that the response will correspond to the following specifications.

Each error from server should be in next format:

  • code: a unique code of an error. Used to identify error from the dictionary.
  • target: some sort of error scope
  • field - the error related to certain field
  • common - the error related to whole request
  • ***message (OPTIONAL)***: the error message for developers (use it only for debug purposes)
  • ***source (OPTIONAL)***: a container for additional data. Arbitrary structure: ( field: resource object attribute name. Required if target set to field. )

Example:

{
"data": [
     {
       "id": 1,
       "userName": "Tom",
       "age": 21
     },
     {
       "id": 2,
       "userName": "Bob",
       "age": 22
     }
   ],
  "errors": [
    {
      "code": "insufficient_funds",
      "target": "common",
      "message": "Hi Nick, it seems that user has empty balance"
    },
    {
      "code": "invalid_punctuation",
      "target": "field",
      "source": {
        "field": "userPassword"
      },
      "message": "Hi Vova, it seems that the password provided is missing a punctuation character"
    },
    {
      "code": "invalid_password_confirmation",
      "target": "field",
      "source": {
        "field": "userPassword",
        "someAdditionalData": "bla bla bla"
      },
      "message": "Hi Lesha, it seems that the password and password confirmation fields do not match"
    }
  ]
 }

####Pagination In server response should be pagination object in the next format:

  • currentPage: current returned page
  • totalPage: total pages amount
  • totlaRecord: total record amount
  • limit: number of items per page

Example:

{
  "data": [
    {
      "id": 1
    },
    {
      "id": 2
    }
  ],
  "pagination": {
    "currentPage": 3,
    "totalPage": 10,
    "totalRecord": 92,
    "limit": 10
  }
}

Version #

0.04

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(response: ApiParserResponse<T>) - returns ApiParserResponse in the states: success , empty or error
  • getParserResponse(response: ApiResponse<T>) - parses the server response object and returns the processed result
  • getErrors(errors: List<ErrorMessage>) - returns a list of processed errors
  • getMessageFromCode(errorCode: String) - returns the message associated with this error code
  • getMessage(errorMessage: ErrorMessage) - returns the processed error
  • getFirstMessage(errors: List<ErrorMessage>) - 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;
    }
}
                            

License #

ApiParser is released under the MIT license. See LICENSE for details.

[0.0.4] - TODO: Add release date. #

added pagination logic

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  api_error_parser: ^0.0.4

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:api_error_parser/api_error_parser.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
51
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
70
Overall:
Weighted score of the above. [more]
69
Learn more about scoring.

We analyzed this package on Jul 9, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.14
  • Flutter: 1.17.5

Analysis suggestions

Package not compatible with SDK dart

Because:

  • api_error_parser that is a package requiring null.

Health suggestions

Format lib/api_parser.dart.

Run flutter format to format lib/api_parser.dart.

Format lib/entity/api_response/api_response_entity.dart.

Run flutter format to format lib/entity/api_response/api_response_entity.dart.

Format lib/entity/api_response/api_response_pagination_entity.dart.

Run flutter format to format lib/entity/api_response/api_response_pagination_entity.dart.

Fix additional 3 files with analysis or formatting issues.

Additional issues in the following files:

  • lib/entity/api_response/pagination_entity.dart (Run flutter format to format lib/entity/api_response/pagination_entity.dart.)
  • lib/entity/parser_response/api_parser_response.dart (Run flutter format to format lib/entity/parser_response/api_parser_response.dart.)
  • lib/entity/parser_response/parser_response_entity.dart (Run flutter format to format lib/entity/parser_response/parser_response_entity.dart.)

Maintenance issues and suggestions

Make sure dartdoc successfully runs on your package's source files. (-10 points)

exitCode: 255 stdout: Documenting api_error_parser... Initialized dartdoc with 29 libraries in 28.6 seconds Generating docs for library api_error_parser from package:api_error_parser/api_error_parser.dart... Generating docs for library api_error_parser from package:api_error_parser/api_parser.dart... Generating docs for library api_parser_response from package:api_error_parser/entity/parser_response/api_parser_response.dart... Generating docs for library api_response_entity from package:api_error_parser/entity/api_response/api_response_entity.dart... Generating docs for library api_response_pagination_entity from package:api_error_parser/entity/api_response/api_response_pagination_entity.dart... Generating docs for library error_message_entity from package:api_error_parser/entity/api_response/error_message_entity.dart... Generating docs for library error_source_entity from package:api_error_parser/entity/api_response/error_source_entity.dart... Generating docs for library pagination_entity from package:api_error_parser/entity/api_response/pagination_entity.dart... Generating docs for library parser_message_entity from package:api_error_parser/entity/parser_response/parser_message_entity.dart... Generating docs for library parser_response_entity from package:api_error_parser/entity/parser_response/parser_response_entity.dart... Validating docs... Documented 10 public libraries in 5.5 seconds stderr: warning: api_error_parser has no library level documentation comments, from api_error_parser: (file:///tmp/pub-dartlang-dartdocOBMHMJ/pkg/lib/api_error_parser.dart:1:9) warning: api_error_parser has no library level documentation comments, from api_error_parser: (file:///tmp/pub-dartlang-dartdocOBMHMJ/pkg/lib/api_parser.dart:1:9) error: failed to write file at: api_error_parser/api_error_parser-library.html for symbol api_error_parser: (file:///tmp/pub-dartlang-dartdocOBMHMJ/pkg/lib/api_parser.dart:1:9) conflicting with file already generated by api_error_parser: (file:///tmp/pub-dartlang-dartdocOBMHMJ/pkg/lib/api_error_parser.dart:1:9) Dartdoc generates a path and filename to write to for each symbol. api_error_parser conflicts with another symbol in the generated path, and therefore can not be written out. Changing the name, library name, or class name (if appropriate) of one of the conflicting items can resolve the conflict. Alternatively, use the @nodoc tag in one symbol's documentation comments to hide it. warning: dartdoc generated a broken link to: LICENSE, linked to from package-api_error_parser: file:///tmp/pub-dartlang-dartdocOBMHMJ/pkg found 3 warnings and 1 error Unhandled exception: dartdoc encountered 1 errors while processing. #0 Dartdoc.generateDocs (package:dartdoc/dartdoc.dart:225:9)

Maintain an example. (-10 points)

Create a short demo in the example/ directory to show how to use this package.

Common filename patterns include main.dart, example.dart, and api_error_parser.dart. Packages with multiple examples should provide example/README.md.

For more information see the pub package layout conventions.

Package is pre-v0.1 release. (-10 points)

While nothing is inherently wrong with versions of 0.0.*, it might mean that the author is still experimenting with the general direction of the API.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.12 1.14.13
meta 1.1.8 1.2.2
sky_engine 0.0.99
typed_data 1.1.6 1.2.0
vector_math 2.0.8 2.1.0-nullsafety
Dev dependencies
flutter_test