UNPKG

@tsailab/xai

Version:

The loto-xai is an openai nodejs sdk compatible extension library.

88 lines 3.06 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SseFetchError = void 0; const error_1 = require("../../error"); /** * */ class SseFetchError extends error_1.XaiError { code; constructor(code, message) { super(message); this.code = code; this.setCode(code); } /** * get error message * @param messages if has messages map,will translate code to message with customize. * @returns string */ getErrorMessage(messages) { let msg = this.code !== undefined ? `[${this.code}] ${this.message}` : `${this.message}`; if (messages && messages[this.code]) { msg = messages[this.code]; } return msg; } static createFromError(err, messages) { if (typeof err === 'string') { return new SseFetchError(error_1.SSE_ERROE_CODE, err); } else if (typeof err === 'object') { const code = err?.code; const msg = err.message; return new SseFetchError(code ?? error_1.SSE_ERROE_CODE, msg); } else if (typeof err === 'number') { const errmsg = messages ? messages[err] : error_1.xaiErrorMessages[err]; return new SseFetchError(err, errmsg ?? 'Unknow Error'); } else { return new SseFetchError(error_1.XAI_ERROR_CODE_400, 'Construct XaiError illegal.'); } } /** * * @param status will reffer api httpStatus Or Response Data code * @param message will reffer api statusText Or Response Data message * @returns SseFetchError */ static newClientError(status, message) { return new SseFetchError(status, message ?? error_1.xaiErrorMessages[status] ?? `Client unknow error[${status}].`); } static newSseError(status, statusText) { return new SseFetchError(status, statusText); } /** * * @param error api response data * CommonResponse * @returns SseFetchError */ static fromSseErrorData(error) { if (typeof error === 'object') { const { code, message } = error; if (!code && !message) return new SseFetchError(564, JSON.stringify(error)); return new SseFetchError(code ?? error_1.SSE_ERROE_CODE, message ?? code); } else if (typeof error === 'string') { try { const { code, message } = JSON.parse(error); if (!code && !message) return new SseFetchError(error_1.SSE_ERROE_CODE, JSON.stringify(error)); return new SseFetchError(code ?? error_1.SSE_ERROE_CODE, message ?? code); } catch (_e) { return new SseFetchError(error_1.SSE_ERROE_CODE, error); } } return new SseFetchError(error_1.SSE_ERROE_CODE, String(error)); } } exports.SseFetchError = SseFetchError; //# sourceMappingURL=streamable.error.js.map