|
| 1 | +import errorParser from '@/utils/error-parser' |
| 2 | + |
| 3 | +describe('utils/error-parser', () => { |
| 4 | + |
| 5 | + it('should parse HTTP 400 error', () => { |
| 6 | + const error = { |
| 7 | + response: { |
| 8 | + status: 400, |
| 9 | + data: { |
| 10 | + result: 'failure', |
| 11 | + message: 'This is an error' |
| 12 | + } |
| 13 | + } |
| 14 | + } |
| 15 | + const parsed = errorParser.parse(error) |
| 16 | + expect(parsed.message).toEqual('This is an error') |
| 17 | + }) |
| 18 | + |
| 19 | + it('should parse HTTP 400 unit test error', () => { |
| 20 | + const error = { |
| 21 | + response: { |
| 22 | + status: 400, |
| 23 | + data: { |
| 24 | + message: 'This is a bad request' |
| 25 | + } |
| 26 | + } |
| 27 | + } |
| 28 | + const parsed = errorParser.parse(error) |
| 29 | + expect(parsed.message).toEqual('This is a bad request') |
| 30 | + }) |
| 31 | + |
| 32 | + it('should parse HTTP 400 no message error', () => { |
| 33 | + const error = { |
| 34 | + response: { |
| 35 | + status: 400 |
| 36 | + } |
| 37 | + } |
| 38 | + const parsed = errorParser.parse(error) |
| 39 | + expect(parsed.message).toEqual('Bad request') |
| 40 | + }) |
| 41 | + |
| 42 | + it('should parse HTTP 401 error', () => { |
| 43 | + const error = { |
| 44 | + response: { |
| 45 | + status: 401, |
| 46 | + statusText: 'Unauthorized' |
| 47 | + } |
| 48 | + } |
| 49 | + const parsed = errorParser.parse(error) |
| 50 | + expect(parsed.message).toEqual('Request not authorized.') |
| 51 | + }) |
| 52 | + |
| 53 | + it('should parse HTTP 403 error', () => { |
| 54 | + const error = { |
| 55 | + response: { |
| 56 | + status: 403, |
| 57 | + statusText: 'Forbidden' |
| 58 | + } |
| 59 | + } |
| 60 | + const parsed = errorParser.parse(error) |
| 61 | + expect(parsed.message).toEqual('Request forbidden.') |
| 62 | + }) |
| 63 | + |
| 64 | + it('should parse HTTP 404 error', () => { |
| 65 | + const error = { |
| 66 | + response: { |
| 67 | + status: 404, |
| 68 | + statusText: 'Not Found' |
| 69 | + } |
| 70 | + } |
| 71 | + const parsed = errorParser.parse(error) |
| 72 | + expect(parsed.message).toEqual('Request failed. Request endpoint not found on the server.') |
| 73 | + }) |
| 74 | + |
| 75 | + it('should parse HTTP 500 known error', () => { |
| 76 | + const error = { |
| 77 | + response: { |
| 78 | + status: 500, |
| 79 | + statusText: 'Internal Server Error', |
| 80 | + data: { |
| 81 | + message: 'This is rephrased error message.' |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + const parsed = errorParser.parse(error) |
| 86 | + expect(parsed.message).toEqual('This is rephrased error message. Please try again later.') |
| 87 | + }) |
| 88 | + |
| 89 | + it('should parse HTTP 500 unknown error', () => { |
| 90 | + const error = { |
| 91 | + response: { |
| 92 | + status: 500, |
| 93 | + statusText: 'Internal Server Error' |
| 94 | + } |
| 95 | + } |
| 96 | + const parsed = errorParser.parse(error) |
| 97 | + expect(parsed.message).toEqual('There is an error on the server side. Please try again later.') |
| 98 | + }) |
| 99 | + |
| 100 | + it('should parse HTTP 503 error', () => { |
| 101 | + const error = { |
| 102 | + response: { |
| 103 | + status: 503, |
| 104 | + statusText: 'Service Unavailable' |
| 105 | + } |
| 106 | + } |
| 107 | + const parsed = errorParser.parse(error) |
| 108 | + expect(parsed.message).toEqual('Request failed. Please try again later.') |
| 109 | + }) |
| 110 | + |
| 111 | + it('should parse HTTP 504 error', () => { |
| 112 | + const error = { |
| 113 | + response: { |
| 114 | + status: 504, |
| 115 | + statusText: 'Gateway Timeout' |
| 116 | + } |
| 117 | + } |
| 118 | + const parsed = errorParser.parse(error) |
| 119 | + expect(parsed.message).toEqual('Request failed. Please try again later.') |
| 120 | + }) |
| 121 | + |
| 122 | + it('should parse empty response error', () => { |
| 123 | + const error = { |
| 124 | + request: {} |
| 125 | + } |
| 126 | + const parsed = errorParser.parse(error) |
| 127 | + expect(parsed.message).toEqual('Request failed. No response from the server.') |
| 128 | + }) |
| 129 | + |
| 130 | + it('should parse unknown string error', () => { |
| 131 | + const error = 'Unknown error' |
| 132 | + const parsed = errorParser.parse(error) |
| 133 | + expect(parsed.message).toEqual('Unknown error') |
| 134 | + }) |
| 135 | + |
| 136 | + it('should parse unknown wrapped error', () => { |
| 137 | + const error = new Error('Unknown error') |
| 138 | + const parsed = errorParser.parse(error) |
| 139 | + expect(parsed.message).toEqual('Unknown error') |
| 140 | + }) |
| 141 | +}) |
0 commit comments