1 | ;
|
2 | /**
|
3 | * -------------------------------------------------------------------------------------------
|
4 | * Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License.
|
5 | * See License in the project root for license information.
|
6 | * -------------------------------------------------------------------------------------------
|
7 | */
|
8 | Object.defineProperty(exports, "__esModule", { value: true });
|
9 | exports.GraphErrorHandler = void 0;
|
10 | var tslib_1 = require("tslib");
|
11 | /**
|
12 | * @module GraphErrorHandler
|
13 | */
|
14 | var GraphError_1 = require("./GraphError");
|
15 | /**
|
16 | * @class
|
17 | * Class for GraphErrorHandler
|
18 | */
|
19 | var GraphErrorHandler = /** @class */ (function () {
|
20 | function GraphErrorHandler() {
|
21 | }
|
22 | /**
|
23 | * @private
|
24 | * @static
|
25 | * Populates the GraphError instance with Error instance values
|
26 | * @param {Error} error - The error returned by graph service or some native error
|
27 | * @param {number} [statusCode] - The status code of the response
|
28 | * @returns The GraphError instance
|
29 | */
|
30 | GraphErrorHandler.constructError = function (error, statusCode) {
|
31 | var gError = new GraphError_1.GraphError(statusCode, "", error);
|
32 | if (error.name !== undefined) {
|
33 | gError.code = error.name;
|
34 | }
|
35 | gError.body = error.toString();
|
36 | gError.date = new Date();
|
37 | return gError;
|
38 | };
|
39 | /**
|
40 | * @private
|
41 | * @static
|
42 | * @async
|
43 | * Populates the GraphError instance from the Error returned by graph service
|
44 | * @param {GraphAPIErrorResponse} graphError - The error possibly returned by graph service or some native error
|
45 | * @param {number} statusCode - The status code of the response
|
46 | * @returns A promise that resolves to GraphError instance
|
47 | *
|
48 | * Example error for https://graph.microsoft.com/v1.0/me/events?$top=3&$search=foo
|
49 | * {
|
50 | * "error": {
|
51 | * "code": "SearchEvents",
|
52 | * "message": "The parameter $search is not currently supported on the Events resource.",
|
53 | * "innerError": {
|
54 | * "request-id": "b31c83fd-944c-4663-aa50-5d9ceb367e19",
|
55 | * "date": "2016-11-17T18:37:45"
|
56 | * }
|
57 | * }
|
58 | * }
|
59 | */
|
60 | GraphErrorHandler.constructErrorFromResponse = function (graphError, statusCode) {
|
61 | var error = graphError.error;
|
62 | var gError = new GraphError_1.GraphError(statusCode, error.message);
|
63 | gError.code = error.code;
|
64 | if (error.innerError !== undefined) {
|
65 | gError.requestId = error.innerError["request-id"];
|
66 | gError.date = new Date(error.innerError.date);
|
67 | }
|
68 | gError.body = JSON.stringify(error);
|
69 | return gError;
|
70 | };
|
71 | /**
|
72 | * @public
|
73 | * @static
|
74 | * @async
|
75 | * To get the GraphError object
|
76 | * Reference - https://docs.microsoft.com/en-us/graph/errors
|
77 | * @param {any} [error = null] - The error returned by graph service or some native error
|
78 | * @param {number} [statusCode = -1] - The status code of the response
|
79 | * @param {GraphRequestCallback} [callback] - The graph request callback function
|
80 | * @returns A promise that resolves to GraphError instance
|
81 | */
|
82 | GraphErrorHandler.getError = function (error, statusCode, callback) {
|
83 | if (error === void 0) { error = null; }
|
84 | if (statusCode === void 0) { statusCode = -1; }
|
85 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
86 | var gError;
|
87 | return tslib_1.__generator(this, function (_a) {
|
88 | if (error && error.error) {
|
89 | gError = GraphErrorHandler.constructErrorFromResponse(error, statusCode);
|
90 | }
|
91 | else if (error instanceof Error) {
|
92 | gError = GraphErrorHandler.constructError(error, statusCode);
|
93 | }
|
94 | else {
|
95 | gError = new GraphError_1.GraphError(statusCode);
|
96 | gError.body = error; // if a custom error is passed which is not instance of Error object or a graph API response
|
97 | }
|
98 | if (typeof callback === "function") {
|
99 | callback(gError, null);
|
100 | }
|
101 | else {
|
102 | return [2 /*return*/, gError];
|
103 | }
|
104 | return [2 /*return*/];
|
105 | });
|
106 | });
|
107 | };
|
108 | return GraphErrorHandler;
|
109 | }());
|
110 | exports.GraphErrorHandler = GraphErrorHandler;
|
111 | //# sourceMappingURL=GraphErrorHandler.js.map |
\ | No newline at end of file |