|
1 | 1 | import { SerializedData } from '../serialization/json.js';
|
2 | 2 | import { GeneratedCodeError } from '../internal/errors.js';
|
| 3 | +import util from 'util'; |
| 4 | +import { DataSanitizer } from '../internal/logging.generated'; |
3 | 5 |
|
4 | 6 | export class BoxSdkError extends GeneratedCodeError {
|
5 | 7 | readonly timestamp?: string;
|
@@ -38,14 +40,56 @@ export interface ResponseInfo {
|
38 | 40 | export class BoxApiError extends BoxSdkError {
|
39 | 41 | readonly requestInfo!: RequestInfo;
|
40 | 42 | readonly responseInfo!: ResponseInfo;
|
| 43 | + readonly dataSanitizer: DataSanitizer = new DataSanitizer({}); |
41 | 44 | constructor(
|
42 | 45 | fields: Pick<
|
43 | 46 | BoxApiError,
|
44 |
| - 'message' | 'timestamp' | 'error' | 'requestInfo' | 'responseInfo' |
| 47 | + | 'message' |
| 48 | + | 'timestamp' |
| 49 | + | 'error' |
| 50 | + | 'requestInfo' |
| 51 | + | 'responseInfo' |
| 52 | + | 'dataSanitizer' |
45 | 53 | >,
|
46 | 54 | ) {
|
47 | 55 | super(fields);
|
48 | 56 | this.name = 'BoxApiError';
|
| 57 | + if (fields.dataSanitizer) { |
| 58 | + this.dataSanitizer = fields.dataSanitizer; |
| 59 | + } |
49 | 60 | Object.setPrototypeOf(this, BoxApiError.prototype);
|
50 | 61 | }
|
| 62 | + |
| 63 | + [util.inspect.custom]() { |
| 64 | + return this.toString(); |
| 65 | + } |
| 66 | + |
| 67 | + toString(): string { |
| 68 | + return JSON.stringify(this.toJSON(), null, 2); |
| 69 | + } |
| 70 | + |
| 71 | + toJSON() { |
| 72 | + return { |
| 73 | + name: this.name, |
| 74 | + message: this.message, |
| 75 | + timestamp: this.timestamp, |
| 76 | + error: this.error, |
| 77 | + requestInfo: { |
| 78 | + method: this.requestInfo.method, |
| 79 | + url: this.requestInfo.url, |
| 80 | + queryParams: this.requestInfo.queryParams, |
| 81 | + headers: this.dataSanitizer.sanitizeHeaders(this.requestInfo.headers), |
| 82 | + body: this.requestInfo.body, |
| 83 | + }, |
| 84 | + responseInfo: { |
| 85 | + statusCode: this.responseInfo.statusCode, |
| 86 | + headers: this.dataSanitizer.sanitizeHeaders(this.responseInfo.headers), |
| 87 | + body: this.dataSanitizer.sanitizeBody(this.responseInfo.body), |
| 88 | + code: this.responseInfo.code, |
| 89 | + contextInfo: this.responseInfo.contextInfo, |
| 90 | + requestId: this.responseInfo.requestId, |
| 91 | + helpUrl: this.responseInfo.helpUrl, |
| 92 | + }, |
| 93 | + }; |
| 94 | + } |
51 | 95 | }
|
0 commit comments