UNPKG

@configurator/ravendb

Version:
100 lines 4.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ExceptionDispatcher = exports.getError = exports.throwError = exports.printError = void 0; const verror_1 = require("verror"); const HttpUtil_1 = require("../Utility/HttpUtil"); const StatusCode_1 = require("../Http/StatusCode"); const Serializer_1 = require("../Mapping/Json/Serializer"); const os = require("os"); function printError(err) { return verror_1.VError.fullStack(err); } exports.printError = printError; function throwError(errName = "RavenException", message, errCause, info) { throw getError(errName, message, errCause, info); } exports.throwError = throwError; function getError(errName = "RavenException", message = "", errCause, info) { const error = new verror_1.VError({ name: errName, cause: errCause, info }, message.replace(/%/g, "%%")); return error; } exports.getError = getError; class ExceptionDispatcher { static get(schema, code, inner) { const message = schema.message; const typeAsString = schema.type; if (code === StatusCode_1.StatusCodes.Conflict) { if (typeAsString.indexOf("DocumentConflictException") !== -1) { return getError("DocumentConflictException", message, inner); } return getError("ConcurrencyException", schema.error, inner); } const error = schema.error + os.EOL + "The server at " + schema.url + " responded with status code: " + code; const determinedType = this._getType(typeAsString); return getError(determinedType || "RavenException", error, inner); } static throwException(response, body) { if (!response) { throw getError("InvalidArgumentException", "Response cannot be null"); } let errorToThrow; try { const json = body; const schema = ExceptionDispatcher._jsonSerializer.deserialize(json); if (response.status === StatusCode_1.StatusCodes.Conflict) { errorToThrow = this._getConflictError(schema, json); } else { const determinedType = this._getType(schema.type); errorToThrow = getError(determinedType || "RavenException", schema.error); } ExceptionDispatcher._fillException(errorToThrow, schema); } catch (errThrowing) { errorToThrow = getError("RavenException", errThrowing.message, errThrowing); } finally { (0, HttpUtil_1.closeHttpResponse)(response); } throw errorToThrow; } static _fillException(exception, json) { if (exception.name === "RavenTimeoutException") { exception.failImmediately = !!json.FailImmediately; } } static _getConflictError(schema, json) { if (schema.type.includes("DocumentConflictException")) { return getError("DocumentConflictException", schema.message, null, { json }); } if (schema.type.includes("ClusterTransactionConcurrencyException")) { return getError("ClusterTransactionConcurrencyException", schema.message, null, { json }); } return getError("ConcurrencyException", schema.message); } static _getType(typeAsString) { if ("System.TimeoutException" === typeAsString) { return "TimeoutException"; } const prefix = "Raven.Client.Exceptions."; if (typeAsString && typeAsString.startsWith(prefix)) { const exceptionName = typeAsString.substring(prefix.length); if (exceptionName.indexOf(".") !== -1) { const tokens = exceptionName.split("."); return tokens[tokens.length - 1]; } return exceptionName; } else { return null; } } } exports.ExceptionDispatcher = ExceptionDispatcher; ExceptionDispatcher._jsonSerializer = Serializer_1.JsonSerializer.getDefaultForCommandPayload(); //# sourceMappingURL=index.js.map