ServiceError.fromJson constructor

ServiceError.fromJson(
  1. Map<String, dynamic> json
)

Creates a ServiceError from a JSON response.

Parses the error information from a service response and creates the appropriate ServiceError instance.

json A map containing 'message' and 'code' fields.

Returns a ServiceError parsed from the JSON data.

Example

final json = {'message': 'User not found', 'code': 2001};
final error = ServiceError.fromJson(json);

Implementation

factory ServiceError.fromJson(Map<String, dynamic> json) {
  final code =
      ServiceErrorCode.fromValue(json['code'] as int) ??
      ServiceErrorCode.internal;
  return ServiceError(json['message'] as String, code);
}