superrouteErrorFrom function
The SuperrouteException of a refused answer: its code, message, field errors, reasons, data, request id (the
body's request_id, else requestId, else the X-Request-Id of headers), headers and Retry-After.
Implementation
SuperrouteException superrouteErrorFrom(int status, Object? body, String? requestId, {Map<String, String> headers = const {}}) {
final map = body is Map ? body : const {};
final rawCode = map['error'] ?? map['code'];
String? code = rawCode is String ? rawCode : (rawCode == null ? null : '$rawCode');
final message = map['message'];
if (code == null && message is String && RegExp(r'^[A-Z_]+$').hasMatch(message)) {
code = message;
}
final description = map['error_description'];
final text = (description is String ? description : null) ??
(message is List ? message.join(' ') : message is String ? message : null) ??
(map['title'] is String ? map['title'] as String : null);
return SuperrouteException(
status: status,
code: code,
message: text,
body: body,
requestId: map['request_id'] is String ? map['request_id'] as String : (requestId ?? _lowerCaseHeaders(headers)['x-request-id']),
errors: _fieldErrors(map['errors']),
reasons: map['reasons'],
data: map['data'],
headers: headers,
);
}