|
| 1 | +import { BadRequestException, NotFoundException } from '@nestjs/common'; |
| 2 | +import { CloudOauthMisconfigurationException } from 'src/modules/cloud/auth/exceptions'; |
| 3 | +import { AxiosError, AxiosHeaders } from 'axios'; |
| 4 | +import { mockSessionMetadata } from 'src/__mocks__'; |
| 5 | +import { getOriginalErrorCause, sanitizeError, sanitizeErrors } from './logsFormatter'; |
| 6 | + |
| 7 | +const simpleError = new Error('Original error'); |
| 8 | +simpleError['some'] = 'field'; |
| 9 | +const errorWithCause = new NotFoundException('Not found', { cause: simpleError }); |
| 10 | +const errorWithCauseDepth2 = new BadRequestException('Bad req', { cause: errorWithCause }); |
| 11 | +const errorWithCauseDepth3 = new CloudOauthMisconfigurationException('Misconfigured', { cause: errorWithCauseDepth2 }); |
| 12 | +const axiosError = new AxiosError( |
| 13 | + 'Request failed with status code 404', |
| 14 | + 'NOT_FOUND', |
| 15 | + { |
| 16 | + method: 'get', |
| 17 | + url: '/test-endpoint', |
| 18 | + headers: AxiosHeaders.prototype, |
| 19 | + data: null, |
| 20 | + }, |
| 21 | + null, |
| 22 | + { |
| 23 | + status: 404, |
| 24 | + statusText: 'Not Found', |
| 25 | + headers: {}, |
| 26 | + config: { |
| 27 | + headers: AxiosHeaders.prototype, |
| 28 | + }, |
| 29 | + data: { |
| 30 | + message: 'Resource not found', |
| 31 | + }, |
| 32 | + }, |
| 33 | +); |
| 34 | + |
| 35 | +const mockLogData: any = { |
| 36 | + sessionMetadata: mockSessionMetadata, |
| 37 | + error: errorWithCauseDepth3, |
| 38 | + data: [ |
| 39 | + errorWithCauseDepth2, |
| 40 | + { |
| 41 | + any: [ |
| 42 | + 'other', |
| 43 | + { |
| 44 | + possible: 'data', |
| 45 | + with: [ |
| 46 | + 'nested', |
| 47 | + 'structure', |
| 48 | + errorWithCause, |
| 49 | + { |
| 50 | + error: simpleError, |
| 51 | + }, |
| 52 | + ], |
| 53 | + }, |
| 54 | + ], |
| 55 | + }, |
| 56 | + ], |
| 57 | +}; |
| 58 | +mockLogData.data.push({ circular: mockLogData.data }); |
| 59 | + |
| 60 | +describe('logsFormatter', () => { |
| 61 | + describe('getOriginalErrorCause', () => { |
| 62 | + it('should return last cause in the chain', () => { |
| 63 | + expect(getOriginalErrorCause(errorWithCauseDepth3)).toEqual(simpleError); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should return undefined if input is not an Error instance', () => { |
| 67 | + expect(getOriginalErrorCause({ cause: simpleError })).toEqual(undefined); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should not fail if input is not specified', () => { |
| 71 | + expect(getOriginalErrorCause(undefined)).toEqual(undefined); |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + describe('sanitizeError', () => { |
| 76 | + it('should sanitize simple error and return only message', () => { |
| 77 | + expect(sanitizeError(simpleError, { omitSensitiveData: true })).toEqual({ |
| 78 | + type: 'Error', |
| 79 | + message: simpleError.message, |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should sanitize simple error and return message (with stack)', () => { |
| 84 | + expect(sanitizeError(simpleError)).toEqual({ |
| 85 | + type: 'Error', |
| 86 | + message: simpleError.message, |
| 87 | + stack: simpleError.stack, |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should return sanitized object with a single original cause for nested errors', () => { |
| 92 | + expect(sanitizeError(errorWithCauseDepth3, { omitSensitiveData: true })).toEqual({ |
| 93 | + type: 'CloudOauthMisconfigurationException', |
| 94 | + message: errorWithCauseDepth3.message, |
| 95 | + cause: { |
| 96 | + type: 'Error', |
| 97 | + message: simpleError.message, |
| 98 | + }, |
| 99 | + }); |
| 100 | + }); |
| 101 | + |
| 102 | + it('should return sanitized object with a single original cause for nested errors (with stack)', () => { |
| 103 | + expect(sanitizeError(errorWithCauseDepth3)).toEqual({ |
| 104 | + type: 'CloudOauthMisconfigurationException', |
| 105 | + message: errorWithCauseDepth3.message, |
| 106 | + stack: errorWithCauseDepth3.stack, |
| 107 | + cause: { |
| 108 | + type: 'Error', |
| 109 | + message: simpleError.message, |
| 110 | + stack: simpleError.stack, |
| 111 | + }, |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + it('should sanitize axios error and return only message when sensitive data is omitted', () => { |
| 116 | + expect(sanitizeError(axiosError, { omitSensitiveData: true })).toEqual({ |
| 117 | + type: 'AxiosError', |
| 118 | + message: axiosError.message, |
| 119 | + }); |
| 120 | + }); |
| 121 | + }); |
| 122 | + |
| 123 | + describe('sanitizeErrors', () => { |
| 124 | + it('should sanitize all errors and replace circular dependencies', () => { |
| 125 | + expect(sanitizeErrors(mockLogData, { omitSensitiveData: true })).toEqual({ |
| 126 | + sessionMetadata: mockSessionMetadata, |
| 127 | + error: { |
| 128 | + type: 'CloudOauthMisconfigurationException', |
| 129 | + message: 'Misconfigured', |
| 130 | + cause: { |
| 131 | + message: 'Original error', |
| 132 | + type: 'Error', |
| 133 | + }, |
| 134 | + }, |
| 135 | + data: [ |
| 136 | + { |
| 137 | + type: 'BadRequestException', |
| 138 | + message: 'Bad req', |
| 139 | + cause: { |
| 140 | + type: 'Error', |
| 141 | + message: 'Original error', |
| 142 | + }, |
| 143 | + }, |
| 144 | + { |
| 145 | + any: [ |
| 146 | + 'other', |
| 147 | + { |
| 148 | + possible: 'data', |
| 149 | + with: [ |
| 150 | + 'nested', |
| 151 | + 'structure', |
| 152 | + { |
| 153 | + cause: { |
| 154 | + message: 'Original error', |
| 155 | + type: 'Error', |
| 156 | + }, |
| 157 | + message: 'Not found', |
| 158 | + type: 'NotFoundException', |
| 159 | + }, |
| 160 | + { |
| 161 | + error: { |
| 162 | + message: 'Original error', |
| 163 | + type: 'Error', |
| 164 | + }, |
| 165 | + }, |
| 166 | + ], |
| 167 | + }, |
| 168 | + ], |
| 169 | + }, |
| 170 | + { |
| 171 | + circular: '[Circular]', |
| 172 | + }, |
| 173 | + ], |
| 174 | + }); |
| 175 | + }); |
| 176 | + }); |
| 177 | +}); |
0 commit comments