@scayle/storefront-nuxt
Version:
Nuxt integration for the SCAYLE Commerce Engine and Storefront API
31 lines (30 loc) • 1.17 kB
JavaScript
import { HttpStatusCode, HttpStatusMessage } from "@scayle/storefront-core";
import { FetchError } from "ofetch";
const parseErrorData = (error) => {
if (error instanceof FetchError) {
const url = error.request?.url ?? error.request?.toString() ?? "/";
return {
statusCode: error.status ?? HttpStatusCode.INTERNAL_SERVER_ERROR,
statusMessage: error.statusText ?? HttpStatusMessage.INTERNAL_SERVER_ERROR,
url
};
}
if (error instanceof Error) {
return {
statusCode: HttpStatusCode.INTERNAL_SERVER_ERROR,
statusMessage: error.message ?? HttpStatusMessage.INTERNAL_SERVER_ERROR
};
}
return {
statusCode: HttpStatusCode.INTERNAL_SERVER_ERROR,
statusMessage: HttpStatusMessage.INTERNAL_SERVER_ERROR
};
};
const resolveError = (error) => {
const { statusCode, statusMessage } = parseErrorData(error);
const [key] = Object.entries(HttpStatusCode).find(([_, code]) => code === statusCode) ?? [];
const statusCodeKey = key;
const name = statusCodeKey ? HttpStatusMessage[statusCodeKey] : HttpStatusMessage.INTERNAL_SERVER_ERROR;
return { statusCode, statusMessage, name };
};
export { resolveError };