ravendb
Version:
RavenDB client for Node.js
102 lines • 4.07 kB
JavaScript
import { closeHttpResponse } from "../Utility/HttpUtil.js";
import { StatusCodes } from "../Http/StatusCode.js";
import { JsonSerializer } from "../Mapping/Json/Serializer.js";
import { EOL } from "../Utility/OsUtil.js";
export function throwError(errName = "RavenException", message, errCause, info) {
throw getError(errName, message, errCause, info);
}
function buildMessageWithInner(error) {
if (!error) {
return null;
}
if (!error.cause) {
return error.message;
}
const inner = buildMessageWithInner(error.cause);
return inner ? error.message + ": " + inner : error.message;
}
export function getError(errName = "RavenException", message = "", errCause, info) {
const innerMessage = buildMessageWithInner(errCause);
const error = new Error(message + (innerMessage ? ": " + innerMessage : ""), { cause: errCause });
error.name = errName;
if (info) {
for (const value of Object.entries(info)) {
error[value[0]] = value[1];
}
}
return error;
}
export class ExceptionDispatcher {
static _jsonSerializer = JsonSerializer.getDefaultForCommandPayload();
static get(schema, code, inner) {
const message = schema.message;
const typeAsString = schema.type;
if (code === StatusCodes.Conflict) {
if (typeAsString.includes("DocumentConflictException")) {
return getError("DocumentConflictException", message, inner);
}
return getError("ConcurrencyException", schema.error, inner);
}
const error = schema.error + 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 === 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 {
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.includes(".")) {
const tokens = exceptionName.split(".");
return tokens.at(-1);
}
return exceptionName;
}
else {
return null;
}
}
}
//# sourceMappingURL=index.js.map