1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const verror_1 = require("verror");
|
4 | const HttpUtil_1 = require("./../Utility/HttpUtil");
|
5 | const StatusCode_1 = require("../Http/StatusCode");
|
6 | const Serializer_1 = require("../Mapping/Json/Serializer");
|
7 | const os = require("os");
|
8 | function printError(err) {
|
9 | return verror_1.VError.fullStack(err);
|
10 | }
|
11 | exports.printError = printError;
|
12 | function throwError(errName = "RavenException", message, errCause, info) {
|
13 | throw getError(errName, message, errCause, info);
|
14 | }
|
15 | exports.throwError = throwError;
|
16 | function 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 | }
|
24 | exports.getError = getError;
|
25 | class 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 | }
|
89 | ExceptionDispatcher._jsonSerializer = Serializer_1.JsonSerializer.getDefaultForCommandPayload();
|
90 | exports.ExceptionDispatcher = ExceptionDispatcher;
|