UNPKG

4.46 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 });
9var tslib_1 = require("tslib");
10/**
11 * @module GraphErrorHandler
12 */
13var GraphError_1 = require("./GraphError");
14/**
15 * @class
16 * Class for GraphErrorHandler
17 */
18var GraphErrorHandler = /** @class */ (function () {
19 function GraphErrorHandler() {
20 }
21 /**
22 * @private
23 * @static
24 * Populates the GraphError instance with Error instance values
25 * @param {Error} error - The error returned by graph service or some native error
26 * @param {number} [statusCode] - The status code of the response
27 * @returns The GraphError instance
28 */
29 GraphErrorHandler.constructError = function (error, statusCode) {
30 var gError = new GraphError_1.GraphError(statusCode, "", error);
31 if (error.name !== undefined) {
32 gError.code = error.name;
33 }
34 gError.body = error.toString();
35 gError.date = new Date();
36 return gError;
37 };
38 /**
39 * @private
40 * @static
41 * @async
42 * Populates the GraphError instance from the Error returned by graph service
43 * @param {any} error - The error returned by graph service or some native error
44 * @param {number} statusCode - The status code of the response
45 * @returns A promise that resolves to GraphError instance
46 *
47 * Example error for https://graph.microsoft.com/v1.0/me/events?$top=3&$search=foo
48 * {
49 * "error": {
50 * "code": "SearchEvents",
51 * "message": "The parameter $search is not currently supported on the Events resource.",
52 * "innerError": {
53 * "request-id": "b31c83fd-944c-4663-aa50-5d9ceb367e19",
54 * "date": "2016-11-17T18:37:45"
55 * }
56 * }
57 * }
58 */
59 GraphErrorHandler.constructErrorFromResponse = function (error, statusCode) {
60 error = error.error;
61 var gError = new GraphError_1.GraphError(statusCode, error.message);
62 gError.code = error.code;
63 if (error.innerError !== undefined) {
64 gError.requestId = error.innerError["request-id"];
65 gError.date = new Date(error.innerError.date);
66 }
67 try {
68 gError.body = JSON.stringify(error);
69 }
70 catch (error) {
71 // tslint:disable-line: no-empty
72 }
73 return gError;
74 };
75 /**
76 * @public
77 * @static
78 * @async
79 * To get the GraphError object
80 * @param {any} [error = null] - The error returned by graph service or some native error
81 * @param {number} [statusCode = -1] - The status code of the response
82 * @param {GraphRequestCallback} [callback] - The graph request callback function
83 * @returns A promise that resolves to GraphError instance
84 */
85 GraphErrorHandler.getError = function (error, statusCode, callback) {
86 if (error === void 0) { error = null; }
87 if (statusCode === void 0) { statusCode = -1; }
88 return tslib_1.__awaiter(this, void 0, void 0, function () {
89 var gError;
90 return tslib_1.__generator(this, function (_a) {
91 if (error && error.error) {
92 gError = GraphErrorHandler.constructErrorFromResponse(error, statusCode);
93 }
94 else if (typeof Error !== "undefined" && error instanceof Error) {
95 gError = GraphErrorHandler.constructError(error, statusCode);
96 }
97 else {
98 gError = new GraphError_1.GraphError(statusCode);
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