toResponse method
Decodes a raw service response into a BaseServiceResponse.
The response may be a string, byte array, map, or any other
protocol-specific payload. The response is decoded according to
the configured response encoding type.
If statusCode indicates a failure (outside the 2xx range),
a ServiceErrorResponse is returned.
For socket-based protocols, ensure that a successful operation
provides a status code in the range 200..299.
throw RPCError if decoding failed.
Implementation
BaseServiceResponse toResponse(Object? response, {int statusCode = 200}) {
if (!ServiceProviderUtils.isSuccessStatusCode(
statusCode,
allowSuccessStatusCodes: successStatusCodes,
)) {
return ServiceProviderUtils.findError(
statusCode: statusCode,
allowStatusCode: errorStatusCodes,
object: response,
);
}
try {
return ServiceSuccessRespose(
statusCode: statusCode,
response: ServiceProviderUtils.ecodeResponse(
body: response,
encoding: responseEncoding,
),
);
} catch (_) {}
throw RPCError(
message: "Parsing response failed.",
request: toJson(),
relatedNetwork: network,
statusCode: statusCode,
details: {
"encoding": responseEncoding.name,
"response": response.runtimeType.toString(),
},
);
}