@eleva-io/erp-sdk
Version:
SDK oficial para el ERP de Eleva
48 lines • 1.97 kB
JavaScript
import { ElevaError } from './errors';
/**
* An `ElevaError` enriched with the context of a failed remote call.
* Not tied to HTTP — covers any outbound call (HTTP, gRPC, message queues, etc.)
* where capturing the outgoing request and incoming response aids debugging.
*
* `ReqM` / `ResM` type the transport metadata for the request and response respectively;
* both default to `Record<string, unknown>` for untyped usage.
*
* @example
* type HttpHeaders = Record<string, string>
* const err = RemoteElevaError.from<ErrorCode, HttpHeaders, HttpHeaders>(body, req, res)
* err.request.metadata?.['authorization'] // string
*/
export class RemoteElevaError extends ElevaError {
/** Context about the outgoing call that produced this error. */
request;
/** Context about the response that contained or triggered the error. */
response;
constructor(args) {
super(args);
this.name = 'RemoteElevaError';
this.request = args.request;
this.response = args.response;
}
/** Full serialisation including remote call request and response context. */
toJSON() {
return {
...super.toJSON(),
request: this.request,
response: this.response,
};
}
/** Narrows to `RemoteElevaError<K, ReqM, ResM>`, preserving metadata types on the narrowed type. */
is(code) {
return super.is(code);
}
/**
* Builds a `RemoteElevaError` from a raw response payload and remote call context.
* Delegates payload parsing to `ElevaError._parsePayload()`; malformed payloads become `INTERNAL_SERVER_ERROR`.
* `cause` takes precedence over any cause inferred from the payload.
*/
static from(body, request, response, cause) {
const parsed = ElevaError._parsePayload(body);
return new RemoteElevaError({ ...parsed, cause: cause ?? parsed.cause, request, response });
}
}
//# sourceMappingURL=errors_remote.js.map