UNPKG

3.62 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const verror_1 = require("verror");
4const HttpUtil_1 = require("./../Utility/HttpUtil");
5const StatusCode_1 = require("../Http/StatusCode");
6const Serializer_1 = require("../Mapping/Json/Serializer");
7const os = require("os");
8function printError(err) {
9 return verror_1.VError.fullStack(err);
10}
11exports.printError = printError;
12function throwError(errName = "RavenException", message, errCause, info) {
13 throw getError(errName, message, errCause, info);
14}
15exports.throwError = throwError;
16function getError(errName = "RavenException", message = "", errCause, info) {
17 const error = new verror_1.VError({
18 name: errName,
19 cause: errCause,
20 info
21 }, message.replace(/%/g, "%%"));
22 return error;
23}
24exports.getError = getError;
25class ExceptionDispatcher {
26 static get(schema, code) {
27 const message = schema.message;
28 const typeAsString = schema.type;
29 if (code === StatusCode_1.StatusCodes.Conflict) {
30 if (typeAsString.indexOf("DocumentConflictException") !== -1) {
31 return getError("DocumentConflictException", message);
32 }
33 return getError("ConcurrencyException", schema.error);
34 }
35 const error = schema.error + os.EOL
36 + "The server at " + schema.url + " responded with status code: " + code;
37 const determinedType = this._getType(typeAsString);
38 return getError(determinedType || "RavenException", error);
39 }
40 static throwException(response, body) {
41 if (!response) {
42 throw getError("InvalidArgumentException", "Response cannot be null");
43 }
44 let errorToThrow;
45 try {
46 const json = body;
47 const schema = ExceptionDispatcher._jsonSerializer.deserialize(json);
48 if (response.statusCode === StatusCode_1.StatusCodes.Conflict) {
49 errorToThrow = this._getConflictError(schema, json);
50 }
51 else {
52 const determinedType = this._getType(schema.type);
53 errorToThrow = getError(determinedType || "RavenException", schema.error);
54 }
55 }
56 catch (errThrowing) {
57 errorToThrow = getError("RavenException", errThrowing.message, errThrowing);
58 }
59 finally {
60 HttpUtil_1.closeHttpResponse(response);
61 response.request.abort();
62 }
63 throw errorToThrow;
64 }
65 static _getConflictError(schema, json) {
66 if (schema.type.includes("DocumentConflictException")) {
67 return getError("DocumentConflictException", schema.message, null, { json });
68 }
69 return getError("ConcurrencyException", schema.message);
70 }
71 static _getType(typeAsString) {
72 if ("System.TimeoutException" === typeAsString) {
73 return "TimeoutException";
74 }
75 const prefix = "Raven.Client.Exceptions.";
76 if (typeAsString && typeAsString.startsWith(prefix)) {
77 const exceptionName = typeAsString.substring(prefix.length);
78 if (exceptionName.indexOf(".") !== -1) {
79 const tokens = exceptionName.split(".");
80 return tokens[tokens.length - 1];
81 }
82 return exceptionName;
83 }
84 else {
85 return null;
86 }
87 }
88}
89ExceptionDispatcher._jsonSerializer = Serializer_1.JsonSerializer.getDefaultForCommandPayload();
90exports.ExceptionDispatcher = ExceptionDispatcher;