@bitrix24/b24jssdk
Version:
Bitrix24 REST API JavaScript SDK
108 lines (105 loc) • 2.9 kB
JavaScript
/**
* @package @bitrix24/b24jssdk
* @version 1.0.3
* @copyright (c) 2026 Bitrix24
* @license MIT
* @see https://github.com/bitrix24/b24jssdk
* @see https://bitrix24.github.io/b24jssdk/
*/
import { SdkError } from '../sdk-error.mjs';
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
class AjaxError extends SdkError {
static {
__name(this, "AjaxError");
}
requestInfo;
constructor(params) {
if (params.code === "AUTHORIZE_ERROR" || params.code === "WRONG_AUTH_TYPE") {
params.status = 403;
}
params.description = AjaxError.formatErrorMessage(params);
super(params);
this.name = "AjaxError";
this.requestInfo = params.requestInfo;
this.cleanErrorStack();
}
/**
* Creates AjaxError from HTTP response
* @todo add support v3
*/
static fromResponse(response) {
return new AjaxError({
code: response.data?.error || "JSSDK_INTERNAL_AJAX_ERROR",
description: response.data?.error_description,
status: response.status,
requestInfo: response.config
});
}
/**
* @inheritDoc
*/
static fromException(error, context) {
if (error instanceof AjaxError) return error;
return new AjaxError({
code: context?.code || "JSSDK_INTERNAL_AJAX_ERROR",
status: context?.status || 500,
description: error instanceof Error ? error.message : String(error),
requestInfo: context?.requestInfo,
originalError: error
});
}
/**
* @inheritDoc
*/
toJSON() {
return {
name: this.name,
code: this.code,
message: this.message,
status: this._status,
timestamp: this.timestamp.toISOString(),
requestInfo: this.requestInfo,
stack: this.stack
};
}
/**
* @inheritDoc
*/
toString() {
let output = `[${this.name}] ${this.code} (${this._status}): ${this.message}`;
if (this.requestInfo) {
output += `
Request: ${this.requestInfo?.requestId ? `[${this.requestInfo.requestId}] ` : ""}${this.requestInfo.method} ${this.requestInfo.url}`;
}
if (this.stack) {
output += `
Stack trace:
${this.stack}`;
}
return output;
}
/**
* @inheritDoc
*/
static formatErrorMessage(params) {
if (!params?.description) {
if (params.requestInfo?.method && params.requestInfo.url) {
return `${params.code} (on ${params.requestInfo.method}${params.requestInfo?.url ? " " + params.requestInfo.url : ""})`;
} else {
return `Internal ajax error`;
}
}
return `${params.description}`;
}
/**
* @inheritDoc
*/
cleanErrorStack() {
if (typeof this.stack === "string") {
this.stack = this.stack.split("\n").filter((line) => !line.includes("AjaxError.constructor")).join("\n");
}
}
}
export { AjaxError };
//# sourceMappingURL=ajax-error.mjs.map