import { code, List, refkey, StatementList } from "@alloy-js/core";
import * as ts from "@alloy-js/typescript";
import { httpRuntimeTemplateLib } from "../external-packages/ts-http-runtime.js";
export function getRestErrorRefkey() {
return refkey("rest-error", "static-helpers", "class");
}
export function getCreateRestErrorRefkey() {
return refkey("create-rest-error", "static-helpers", "function");
}
export function RestError() {
const requestRefkey = refkey("request", "rest-error-class-field");
const responseRefkey = refkey("response", "rest-error-class-field");
const statusRefkey = refkey("status", "rest-error-class-field");
const bodyRefkey = refkey("body", "rest-error-class-field");
const headersRefkey = refkey("headers", "rest-error-class-field");
const constructorRefkey = refkey("constructor", "rest-error-class-method");
const fromHttpResponseRefkey = refkey("from-http-response", "rest-error-class-method");
const restErrorClass = (
{code`
// Create an error message that includes relevant details.
super(\`$\{message\} - HTTP $\{response.status} received for $\{response.request.method} $\{response.request.url}\`);
this.name = 'RestError';
this.request = response.request;
this.response = response;
this.status = response.status;
this.headers = response.headers;
this.body = response.body;
// Set the prototype explicitly.
Object.setPrototypeOf(this, RestError.prototype);
`}
{code`
const defaultMessage = \`Unexpected HTTP status code: $\{response.status}\`;
return new RestError(defaultMessage, response);
`}
);
const createRestError = (
{code`
return ${fromHttpResponseRefkey}(response);
`}
);
return [restErrorClass, "\n\n", createRestError];
}