UNPKG

@itwin/property-validation-client

Version:
74 lines 3.51 kB
/*--------------------------------------------------------------------------------------------- * Copyright (c) Bentley Systems, Incorporated. All rights reserved. * See LICENSE.md in the project root for license terms and full copyright notice. *--------------------------------------------------------------------------------------------*/ import { ValidationErrorCode } from "./interfaces/ValidationErrorInterfaces"; export function isValidationApiError(error) { var _a; const errorCode = (_a = error) === null || _a === void 0 ? void 0 : _a.code; return errorCode !== undefined && typeof errorCode === "string"; } export class ValidationErrorImpl extends Error { constructor(params) { super(); this.name = this.code = params.code; this.message = params.message; this.details = params.details; } } export class ValidationErrorParser { static parse(response) { var _a, _b, _c; if (!response.statusCode) { return new ValidationErrorImpl({ code: ValidationErrorCode.Unknown, message: ValidationErrorParser._defaultErrorMessage }); } // TODO: remove the special handling when APIM team fixes incorrect error body if (response.statusCode === 401) { return new ValidationErrorImpl({ code: ValidationErrorCode.Unauthorized, message: "The user is unauthorized. Please provide valid authentication credentials." }); } const errorFromApi = response.body; const errorCode = ValidationErrorParser.parseCode((_a = errorFromApi === null || errorFromApi === void 0 ? void 0 : errorFromApi.error) === null || _a === void 0 ? void 0 : _a.code); const errorDetails = ValidationErrorParser.parseDetails((_b = errorFromApi.error) === null || _b === void 0 ? void 0 : _b.details); const errorMessage = ValidationErrorParser.parseAndFormatMessage((_c = errorFromApi === null || errorFromApi === void 0 ? void 0 : errorFromApi.error) === null || _c === void 0 ? void 0 : _c.message, errorDetails); return new ValidationErrorImpl({ code: errorCode, message: errorMessage, details: errorDetails, }); } static parseCode(errorCode) { if (!errorCode) { return ValidationErrorCode.Unrecognized; } let parsedCode = ValidationErrorCode[errorCode]; if (!parsedCode) { parsedCode = ValidationErrorCode.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 : ValidationErrorParser._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; } } ValidationErrorParser._defaultErrorMessage = "Unknown error occurred"; //# sourceMappingURL=ValidationErrorParser.js.map