1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
15 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
16 | };
|
17 | var _a;
|
18 | Object.defineProperty(exports, "__esModule", { value: true });
|
19 | exports.GaxiosError = exports.GAXIOS_ERROR_SYMBOL = void 0;
|
20 | exports.defaultErrorRedactor = defaultErrorRedactor;
|
21 | const url_1 = require("url");
|
22 | const util_1 = require("./util");
|
23 | const extend_1 = __importDefault(require("extend"));
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | exports.GAXIOS_ERROR_SYMBOL = Symbol.for(`${util_1.pkg.name}-gaxios-error`);
|
30 |
|
31 | class GaxiosError extends Error {
|
32 | |
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | static [(_a = exports.GAXIOS_ERROR_SYMBOL, Symbol.hasInstance)](instance) {
|
39 | if (instance &&
|
40 | typeof instance === 'object' &&
|
41 | exports.GAXIOS_ERROR_SYMBOL in instance &&
|
42 | instance[exports.GAXIOS_ERROR_SYMBOL] === util_1.pkg.version) {
|
43 | return true;
|
44 | }
|
45 |
|
46 | return Function.prototype[Symbol.hasInstance].call(GaxiosError, instance);
|
47 | }
|
48 | constructor(message, config, response, error) {
|
49 | var _b;
|
50 | super(message);
|
51 | this.config = config;
|
52 | this.response = response;
|
53 | this.error = error;
|
54 | |
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 | this[_a] = util_1.pkg.version;
|
64 |
|
65 |
|
66 | this.config = (0, extend_1.default)(true, {}, config);
|
67 | if (this.response) {
|
68 | this.response.config = (0, extend_1.default)(true, {}, this.response.config);
|
69 | }
|
70 | if (this.response) {
|
71 | try {
|
72 | this.response.data = translateData(this.config.responseType, (_b = this.response) === null || _b === void 0 ? void 0 : _b.data);
|
73 | }
|
74 | catch (_c) {
|
75 |
|
76 |
|
77 |
|
78 | }
|
79 | this.status = this.response.status;
|
80 | }
|
81 | if (error && 'code' in error && error.code) {
|
82 | this.code = error.code;
|
83 | }
|
84 | if (config.errorRedactor) {
|
85 | config.errorRedactor({
|
86 | config: this.config,
|
87 | response: this.response,
|
88 | });
|
89 | }
|
90 | }
|
91 | }
|
92 | exports.GaxiosError = GaxiosError;
|
93 | function translateData(responseType, data) {
|
94 | switch (responseType) {
|
95 | case 'stream':
|
96 | return data;
|
97 | case 'json':
|
98 | return JSON.parse(JSON.stringify(data));
|
99 | case 'arraybuffer':
|
100 | return JSON.parse(Buffer.from(data).toString('utf8'));
|
101 | case 'blob':
|
102 | return JSON.parse(data.text());
|
103 | default:
|
104 | return data;
|
105 | }
|
106 | }
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 | function defaultErrorRedactor(data) {
|
116 | const REDACT = '<<REDACTED> - See `errorRedactor` option in `gaxios` for configuration>.';
|
117 | function redactHeaders(headers) {
|
118 | if (!headers)
|
119 | return;
|
120 | for (const key of Object.keys(headers)) {
|
121 |
|
122 | if (/^authentication$/i.test(key)) {
|
123 | headers[key] = REDACT;
|
124 | }
|
125 |
|
126 | if (/^authorization$/i.test(key)) {
|
127 | headers[key] = REDACT;
|
128 | }
|
129 |
|
130 | if (/secret/i.test(key)) {
|
131 | headers[key] = REDACT;
|
132 | }
|
133 | }
|
134 | }
|
135 | function redactString(obj, key) {
|
136 | if (typeof obj === 'object' &&
|
137 | obj !== null &&
|
138 | typeof obj[key] === 'string') {
|
139 | const text = obj[key];
|
140 | if (/grant_type=/i.test(text) ||
|
141 | /assertion=/i.test(text) ||
|
142 | /secret/i.test(text)) {
|
143 | obj[key] = REDACT;
|
144 | }
|
145 | }
|
146 | }
|
147 | function redactObject(obj) {
|
148 | if (typeof obj === 'object' && obj !== null) {
|
149 | if ('grant_type' in obj) {
|
150 | obj['grant_type'] = REDACT;
|
151 | }
|
152 | if ('assertion' in obj) {
|
153 | obj['assertion'] = REDACT;
|
154 | }
|
155 | if ('client_secret' in obj) {
|
156 | obj['client_secret'] = REDACT;
|
157 | }
|
158 | }
|
159 | }
|
160 | if (data.config) {
|
161 | redactHeaders(data.config.headers);
|
162 | redactString(data.config, 'data');
|
163 | redactObject(data.config.data);
|
164 | redactString(data.config, 'body');
|
165 | redactObject(data.config.body);
|
166 | try {
|
167 | const url = new url_1.URL('', data.config.url);
|
168 | if (url.searchParams.has('token')) {
|
169 | url.searchParams.set('token', REDACT);
|
170 | }
|
171 | if (url.searchParams.has('client_secret')) {
|
172 | url.searchParams.set('client_secret', REDACT);
|
173 | }
|
174 | data.config.url = url.toString();
|
175 | }
|
176 | catch (_b) {
|
177 |
|
178 | }
|
179 | }
|
180 | if (data.response) {
|
181 | defaultErrorRedactor({ config: data.response.config });
|
182 | redactHeaders(data.response.headers);
|
183 | redactString(data.response, 'data');
|
184 | redactObject(data.response.data);
|
185 | }
|
186 | return data;
|
187 | }
|
188 |
|
\ | No newline at end of file |