UNPKG

@itwin/clash-detection-client

Version:

Clash Detection client for the iTwin platform

70 lines 3.25 kB
import { ClashDetectionErrorCode } from "./interfaces/ClashDetectionErrorInterfaces"; export function isClashDetectionApiError(error) { var _a; const errorCode = (_a = error) === null || _a === void 0 ? void 0 : _a.code; return errorCode !== undefined && typeof errorCode === "string"; } export class ClashDetectionErrorImpl extends Error { constructor(params) { super(); this.name = this.code = params.code; this.message = params.message; this.details = params.details; } } export class ClashDetectionErrorParser { static parse(response) { var _a, _b, _c; if (!response.statusCode) { return new ClashDetectionErrorImpl({ code: ClashDetectionErrorCode.Unknown, message: ClashDetectionErrorParser._defaultErrorMessage }); } // TODO: remove the special handling when APIM team fixes incorrect error body if (response.statusCode === 401) { return new ClashDetectionErrorImpl({ code: ClashDetectionErrorCode.Unauthorized, message: "The user is unauthorized. Please provide valid authentication credentials." }); } const errorFromApi = response.body; const errorCode = ClashDetectionErrorParser.parseCode((_a = errorFromApi === null || errorFromApi === void 0 ? void 0 : errorFromApi.error) === null || _a === void 0 ? void 0 : _a.code); const errorDetails = ClashDetectionErrorParser.parseDetails((_b = errorFromApi.error) === null || _b === void 0 ? void 0 : _b.details); const errorMessage = ClashDetectionErrorParser.parseAndFormatMessage((_c = errorFromApi === null || errorFromApi === void 0 ? void 0 : errorFromApi.error) === null || _c === void 0 ? void 0 : _c.message, errorDetails); return new ClashDetectionErrorImpl({ code: errorCode, message: errorMessage, details: errorDetails, }); } static parseCode(errorCode) { if (!errorCode) { return ClashDetectionErrorCode.Unrecognized; } let parsedCode = ClashDetectionErrorCode[errorCode]; if (!parsedCode) { parsedCode = ClashDetectionErrorCode.Unrecognized; } return parsedCode; } static parseDetails(details) { if (!details) { return undefined; } return details.map((unparsedDetail) => { return { ...unparsedDetail, code: this.parseCode(unparsedDetail.code) }; }); } static parseAndFormatMessage(message, errorDetails) { let result = message !== null && message !== void 0 ? message : ClashDetectionErrorParser._defaultErrorMessage; if (!errorDetails || errorDetails.length === 0) { return result; } result += " Details:\n"; for (let i = 0; i < errorDetails.length; i++) { result += `${i + 1}. ${errorDetails[i].code}: ${errorDetails[i].message}`; if (errorDetails[i].target) { result += ` Target: ${errorDetails[i].target}.`; } result += "\n"; } return result; } } ClashDetectionErrorParser._defaultErrorMessage = "Unknown error occurred"; //# sourceMappingURL=ClashDetectionErrorParser.js.map