UNPKG

@tsailab/xai

Version:

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

84 lines 2.88 kB
import { SSE_ERROE_CODE, XAI_ERROR_CODE_400, XaiError, xaiErrorMessages, } from '../../error'; /** * */ export class SseFetchError extends 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(SSE_ERROE_CODE, err); } else if (typeof err === 'object') { const code = err?.code; const msg = err.message; return new SseFetchError(code ?? SSE_ERROE_CODE, msg); } else if (typeof err === 'number') { const errmsg = messages ? messages[err] : xaiErrorMessages[err]; return new SseFetchError(err, errmsg ?? 'Unknow Error'); } else { return new SseFetchError(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 ?? 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 ?? SSE_ERROE_CODE, message ?? code); } else if (typeof error === 'string') { try { const { code, message } = JSON.parse(error); if (!code && !message) return new SseFetchError(SSE_ERROE_CODE, JSON.stringify(error)); return new SseFetchError(code ?? SSE_ERROE_CODE, message ?? code); } catch (_e) { return new SseFetchError(SSE_ERROE_CODE, error); } } return new SseFetchError(SSE_ERROE_CODE, String(error)); } } //# sourceMappingURL=streamable.error.js.map