transformer<T> method
Future<Response<T>>
transformer<
T>( - Response<T> response
)
override
Implementation
@override
Future<Response<T>> transformer<T>(Response<T> response) async {
if (response.requestOptions.extra.containsKey('isRaw') &&
response.requestOptions.extra['isRaw'] is bool &&
response.requestOptions.extra['isRaw'] == true) {
return response;
}
// Response body structural error
if (!(response.data is Map)) {
throw HttpError(
requestOptions: response.requestOptions,
response: response,
type: DioErrorType.other,
statusCode: response.statusCode ?? 0,
errorCode: HttpErrorCode.unknownError,
errorMessage: 'Response body is not an map.',
);
}
if (!(response.data as Map).containsKey(codeKey)) {
throw HttpError(
requestOptions: response.requestOptions,
response: response,
type: DioErrorType.other,
statusCode: response.statusCode ?? 0,
errorCode: HttpErrorCode.unknownError,
errorMessage: 'The `$codeKey` key does not exist in response body.',
);
}
var data = response.data as Map;
if (!(data[codeKey] is int)) {
throw HttpError(
requestOptions: response.requestOptions,
response: response,
type: DioErrorType.other,
statusCode: response.statusCode ?? 0,
errorCode: HttpErrorCode.unknownError,
errorMessage:
'The `$codeKey` key in the response body is not an [int] type.',
);
}
// Response returns error
if (data[codeKey] != successCode) {
throw HttpError(
requestOptions: response.requestOptions,
response: response,
type: DioErrorType.other,
statusCode: response.statusCode ?? 0,
errorCode: data[codeKey].toString(),
errorMessage: data[messageKey] ?? 'unknown error',
);
}
response.data = data[dataKey] ?? null;
return response;
}