|
3 | 3 | * SPDX-License-Identifier: Apache-2.0 |
4 | 4 | */ |
5 | 5 |
|
| 6 | +import { ApiResponse } from '@opensearch-project/opensearch'; |
| 7 | +import { |
| 8 | + ConnectionError, |
| 9 | + NoLivingConnectionsError, |
| 10 | + ResponseError, |
| 11 | +} from '@opensearch-project/opensearch/lib/errors'; |
6 | 12 | import { SavedObjectsErrorHelpers } from '../../../../../src/core/server'; |
7 | | -import { DataSourceConfigError } from './error'; |
| 13 | +import { createDataSourceError, DataSourceError } from './error'; |
| 14 | +import { errors as LegacyErrors } from 'elasticsearch'; |
8 | 15 |
|
9 | | -describe('DataSourceConfigError', () => { |
10 | | - it('create from savedObject bad request error should be 400 error', () => { |
| 16 | +const createApiResponseError = ({ |
| 17 | + statusCode = 200, |
| 18 | + headers = {}, |
| 19 | + body = {}, |
| 20 | +}: { |
| 21 | + statusCode?: number; |
| 22 | + headers?: Record<string, string>; |
| 23 | + body?: Record<string, any>; |
| 24 | +} = {}): ApiResponse => { |
| 25 | + return { |
| 26 | + body, |
| 27 | + statusCode, |
| 28 | + headers, |
| 29 | + warnings: [], |
| 30 | + meta: {} as any, |
| 31 | + }; |
| 32 | +}; |
| 33 | + |
| 34 | +describe('CreateDataSourceError', () => { |
| 35 | + it('create from savedObject bad request error should generate 400 error', () => { |
11 | 36 | const error = SavedObjectsErrorHelpers.createBadRequestError('test reason message'); |
12 | | - expect(new DataSourceConfigError('test prefix: ', error)).toMatchObject({ |
| 37 | + expect(createDataSourceError(error)).toMatchObject({ |
13 | 38 | statusCode: 400, |
14 | | - message: 'test prefix: test reason message: Bad Request', |
| 39 | + message: 'Data Source Error: test reason message: Bad Request', |
15 | 40 | }); |
16 | 41 | }); |
17 | 42 |
|
18 | | - it('create from savedObject not found error should be 400 error', () => { |
19 | | - const error = SavedObjectsErrorHelpers.decorateNotAuthorizedError(new Error()); |
20 | | - expect(new DataSourceConfigError('test prefix: ', error)).toHaveProperty('statusCode', 400); |
| 43 | + it('create from savedObject not found error should have statusCode 404', () => { |
| 44 | + const error = SavedObjectsErrorHelpers.createGenericNotFoundError('type', 'id'); |
| 45 | + expect(createDataSourceError(error)).toHaveProperty('statusCode', 404); |
21 | 46 | }); |
22 | 47 |
|
23 | | - it('create from savedObject service unavailable error should be a 500 error', () => { |
| 48 | + it('create from savedObject service unavailable error should have statusCode 503', () => { |
24 | 49 | const error = SavedObjectsErrorHelpers.decorateOpenSearchUnavailableError( |
25 | 50 | new Error('test reason message') |
26 | 51 | ); |
27 | | - expect(new DataSourceConfigError('test prefix: ', error)).toMatchObject({ |
28 | | - statusCode: 500, |
29 | | - message: 'test prefix: test reason message', |
| 52 | + expect(createDataSourceError(error)).toMatchObject({ |
| 53 | + statusCode: 503, |
| 54 | + message: 'Data Source Error: test reason message', |
30 | 55 | }); |
31 | 56 | }); |
32 | 57 |
|
33 | 58 | it('create from non savedObject error should always be a 400 error', () => { |
34 | 59 | const error = new Error('test reason message'); |
35 | | - expect(new DataSourceConfigError('test prefix: ', error)).toMatchObject({ |
| 60 | + expect(createDataSourceError(error)).toMatchObject({ |
36 | 61 | statusCode: 400, |
37 | | - message: 'test prefix: test reason message', |
| 62 | + message: 'Data Source Error: test reason message', |
38 | 63 | }); |
39 | 64 | }); |
| 65 | + |
| 66 | + it('create from client response error 401 should be casted to a 400 DataSourceError', () => { |
| 67 | + expect( |
| 68 | + createDataSourceError(new ResponseError(createApiResponseError({ statusCode: 401 }))) |
| 69 | + ).toHaveProperty('statusCode', 400); |
| 70 | + }); |
| 71 | + |
| 72 | + it('create from non 401 client response error should respect original statusCode', () => { |
| 73 | + expect( |
| 74 | + createDataSourceError(new ResponseError(createApiResponseError({ statusCode: 403 }))) |
| 75 | + ).toHaveProperty('statusCode', 403); |
| 76 | + expect( |
| 77 | + createDataSourceError(new ResponseError(createApiResponseError({ statusCode: 404 }))) |
| 78 | + ).toHaveProperty('statusCode', 404); |
| 79 | + expect( |
| 80 | + createDataSourceError(new ResponseError(createApiResponseError({ statusCode: 500 }))) |
| 81 | + ).toHaveProperty('statusCode', 500); |
| 82 | + }); |
| 83 | + |
| 84 | + it('create from non-response client error should be casted to a 400 DataSourceError', () => { |
| 85 | + expect( |
| 86 | + createDataSourceError(new ConnectionError('error', createApiResponseError())) |
| 87 | + ).toHaveProperty('statusCode', 400); |
| 88 | + expect( |
| 89 | + createDataSourceError(new NoLivingConnectionsError('error', createApiResponseError())) |
| 90 | + ).toHaveProperty('statusCode', 400); |
| 91 | + expect(createDataSourceError(new Error('foo'))).toHaveProperty('statusCode', 400); |
| 92 | + }); |
| 93 | + |
| 94 | + it('create from legacy client 401 error should be casted to a 400 DataSourceError', () => { |
| 95 | + expect(createDataSourceError(new LegacyErrors.AuthenticationException())).toEqual( |
| 96 | + new DataSourceError(new Error('dummy'), 'Authentication Exception', 400) |
| 97 | + ); |
| 98 | + }); |
| 99 | + |
| 100 | + it('create from legacy client non 401 error should respect original statusCode', () => { |
| 101 | + expect(createDataSourceError(new LegacyErrors.NotFound())).toEqual( |
| 102 | + new DataSourceError(new Error('dummy'), 'Not Found', 404) |
| 103 | + ); |
| 104 | + expect(createDataSourceError(new LegacyErrors.TooManyRequests())).toEqual( |
| 105 | + new DataSourceError(new Error('dummy'), 'Too Many Requests', 429) |
| 106 | + ); |
| 107 | + expect(createDataSourceError(new LegacyErrors.InternalServerError())).toEqual( |
| 108 | + new DataSourceError(new Error('dummy'), 'Internal Server Error', 400) |
| 109 | + ); |
| 110 | + }); |
| 111 | + |
| 112 | + it('create from legacy client error should be casted to a 400 DataSourceError', () => { |
| 113 | + expect(createDataSourceError(new LegacyErrors.NoConnections())).toEqual( |
| 114 | + new DataSourceError(new Error('dummy'), 'No Living connections', 400) |
| 115 | + ); |
| 116 | + expect(createDataSourceError(new LegacyErrors.ConnectionFault())).toEqual( |
| 117 | + new DataSourceError(new Error('dummy'), 'Connection Failure', 400) |
| 118 | + ); |
| 119 | + }); |
40 | 120 | }); |
0 commit comments