UNPKG

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