@bitrix24/b24jssdk
Version:
Bitrix24 REST API JavaScript SDK
56 lines (54 loc) • 2.24 kB
JavaScript
/**
* @package @bitrix24/b24jssdk
* @version 2.2.0
* @copyright (c) 2026 Bitrix24
* @license MIT
* @see https://github.com/bitrix24/b24jssdk
* @see https://bitrix24.github.io/b24jssdk/
*/
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
const MESSAGE_SEPARATOR = " ";
function parseErrorPayload(body, fallbackCode, fallbackDescription) {
if (!body || typeof body !== "object" || !("error" in body)) {
return void 0;
}
const responseData = body;
if (responseData.error && typeof responseData.error === "object" && "code" in responseData.error) {
const error = responseData.error;
let description = String(error.message ?? "").trimEnd();
if (error.validation && error.validation.length > 0) {
const messages = error.validation.map((row) => row?.message || JSON.stringify(row)).filter((message) => message !== void 0 && message !== "");
if (messages.length > 0) {
if (description.length > 0) {
if (!description.endsWith(".")) {
description += ".";
}
description += MESSAGE_SEPARATOR;
}
description += messages.join(MESSAGE_SEPARATOR);
}
}
return {
code: error.code,
description,
// Kept verbatim rather than reshaped: `field` is what the caller came for,
// and the portal is free to add keys the SDK has not seen. The copy is
// what detaches it from the response body; the freeze stops a caller
// reordering or extending the array. Both are **shallow** — the `readonly`
// on each entry's fields is a type-level claim only, and nothing stops a
// caller writing to `validation[0].field` at runtime.
...error.validation ? { validation: Object.freeze([...error.validation]) } : {}
};
}
if (responseData.error && typeof responseData.error === "string") {
return {
code: responseData.error !== "0" ? responseData.error : fallbackCode,
description: responseData?.error_description ?? fallbackDescription
};
}
return void 0;
}
__name(parseErrorPayload, "parseErrorPayload");
export { parseErrorPayload };
//# sourceMappingURL=parse-error-payload.mjs.map