|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +import { AgentNotFoundError } from '@kbn/fleet-plugin/server'; |
| 9 | +import { |
| 10 | + AgentPolicyNotFoundError, |
| 11 | + PackagePolicyNotFoundError, |
| 12 | +} from '@kbn/fleet-plugin/server/errors'; |
| 13 | +import { errors } from '@elastic/elasticsearch'; |
| 14 | +import { EndpointError } from '../../../common/endpoint/errors'; |
| 15 | +import { NotFoundError } from '../errors'; |
| 16 | +import { catchAndWrapError, wrapErrorIfNeeded } from './wrap_errors'; |
| 17 | + |
| 18 | +describe('wrapErrorIfNeeded', () => { |
| 19 | + it('returns the same instance when already an EndpointError', () => { |
| 20 | + const original = new EndpointError('already wrapped'); |
| 21 | + expect(wrapErrorIfNeeded(original)).toBe(original); |
| 22 | + }); |
| 23 | + |
| 24 | + it('wraps a plain Error in EndpointError', () => { |
| 25 | + const original = new Error('plain error'); |
| 26 | + const result = wrapErrorIfNeeded(original); |
| 27 | + |
| 28 | + expect(result).toBeInstanceOf(EndpointError); |
| 29 | + expect(result.message).toBe('plain error'); |
| 30 | + expect(result.meta).toBe(original); |
| 31 | + }); |
| 32 | + |
| 33 | + it('applies messagePrefix to a plain Error', () => { |
| 34 | + const original = new Error('something went wrong'); |
| 35 | + const result = wrapErrorIfNeeded(original, 'context'); |
| 36 | + |
| 37 | + expect(result).toBeInstanceOf(EndpointError); |
| 38 | + expect(result.message).toBe('context: something went wrong'); |
| 39 | + }); |
| 40 | + |
| 41 | + describe('Fleet Not Found errors', () => { |
| 42 | + it('wraps AgentNotFoundError in NotFoundError', () => { |
| 43 | + const original = new AgentNotFoundError('agent not found'); |
| 44 | + const result = wrapErrorIfNeeded(original); |
| 45 | + |
| 46 | + expect(result).toBeInstanceOf(NotFoundError); |
| 47 | + expect(result.message).toBe('agent not found'); |
| 48 | + expect(result.meta).toBe(original); |
| 49 | + }); |
| 50 | + |
| 51 | + it('wraps AgentPolicyNotFoundError in NotFoundError', () => { |
| 52 | + const original = new AgentPolicyNotFoundError('policy not found'); |
| 53 | + const result = wrapErrorIfNeeded(original); |
| 54 | + |
| 55 | + expect(result).toBeInstanceOf(NotFoundError); |
| 56 | + expect(result.message).toBe('policy not found'); |
| 57 | + }); |
| 58 | + |
| 59 | + it('wraps PackagePolicyNotFoundError in NotFoundError', () => { |
| 60 | + const original = new PackagePolicyNotFoundError('package policy not found'); |
| 61 | + const result = wrapErrorIfNeeded(original); |
| 62 | + |
| 63 | + expect(result).toBeInstanceOf(NotFoundError); |
| 64 | + expect(result.message).toBe('package policy not found'); |
| 65 | + }); |
| 66 | + |
| 67 | + it('applies messagePrefix to Fleet Not Found errors', () => { |
| 68 | + const original = new AgentNotFoundError('agent-123'); |
| 69 | + const result = wrapErrorIfNeeded(original, 'fetch agent'); |
| 70 | + |
| 71 | + expect(result).toBeInstanceOf(NotFoundError); |
| 72 | + expect(result.message).toBe('fetch agent: agent-123'); |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + describe('Elasticsearch errors', () => { |
| 77 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 78 | + const buildResponseError = (body: any, statusCode = 404) => |
| 79 | + new errors.ResponseError({ |
| 80 | + body, |
| 81 | + statusCode, |
| 82 | + headers: {}, |
| 83 | + warnings: null, |
| 84 | + // @ts-expect-error |
| 85 | + meta: { |
| 86 | + body, |
| 87 | + statusCode, |
| 88 | + headers: {}, |
| 89 | + context: {}, |
| 90 | + request: { |
| 91 | + params: { method: 'GET', path: '/_search', querystring: {}, body: undefined }, |
| 92 | + options: {}, |
| 93 | + id: 'test-request', |
| 94 | + }, |
| 95 | + attempts: 1, |
| 96 | + aborted: false, |
| 97 | + } as unknown as errors.ResponseError['meta'], |
| 98 | + }); |
| 99 | + |
| 100 | + it('wraps ElasticsearchClientError in EndpointError', () => { |
| 101 | + const esError = buildResponseError({ error: { type: 'index_not_found_exception' } }); |
| 102 | + const result = wrapErrorIfNeeded(esError); |
| 103 | + |
| 104 | + expect(result).toBeInstanceOf(EndpointError); |
| 105 | + }); |
| 106 | + |
| 107 | + it('builds a descriptive message from ES response reason fields', () => { |
| 108 | + const esError = buildResponseError({ |
| 109 | + error: { |
| 110 | + type: 'index_not_found_exception', |
| 111 | + reason: 'no such index [.fleet-agents]', |
| 112 | + index: '.fleet-agents', |
| 113 | + }, |
| 114 | + }); |
| 115 | + const result = wrapErrorIfNeeded(esError); |
| 116 | + |
| 117 | + expect(result.message).toContain('no such index [.fleet-agents]'); |
| 118 | + }); |
| 119 | + |
| 120 | + it('applies messagePrefix to ES errors', () => { |
| 121 | + const esError = buildResponseError({ |
| 122 | + error: { type: 'search_phase_execution_exception', reason: 'shard failed' }, |
| 123 | + }); |
| 124 | + const result = wrapErrorIfNeeded(esError, 'search failed'); |
| 125 | + |
| 126 | + expect(result.message).toMatch(/^search failed:/); |
| 127 | + }); |
| 128 | + |
| 129 | + it('populates debug.es_request with request parameters', () => { |
| 130 | + const esError = buildResponseError({ error: { reason: 'not found' } }); |
| 131 | + const result = wrapErrorIfNeeded(esError); |
| 132 | + |
| 133 | + expect(result.debug).toBeDefined(); |
| 134 | + expect(result.debug.es_request).toMatchObject({ |
| 135 | + method: 'GET', |
| 136 | + path: '/_search', |
| 137 | + }); |
| 138 | + }); |
| 139 | + }); |
| 140 | +}); |
| 141 | + |
| 142 | +describe('catchAndWrapError', () => { |
| 143 | + it('rejects with an EndpointError wrapping the original error', async () => { |
| 144 | + const original = new Error('async failure'); |
| 145 | + await expect(Promise.reject(original).catch(catchAndWrapError)).rejects.toBeInstanceOf( |
| 146 | + EndpointError |
| 147 | + ); |
| 148 | + }); |
| 149 | + |
| 150 | + it('rejects with the same EndpointError when already one', async () => { |
| 151 | + const original = new EndpointError('already wrapped'); |
| 152 | + await expect(Promise.reject(original).catch(catchAndWrapError)).rejects.toBe(original); |
| 153 | + }); |
| 154 | + |
| 155 | + describe('withMessage', () => { |
| 156 | + it('rejects with an EndpointError whose message includes the custom prefix', async () => { |
| 157 | + const original = new Error('downstream failure'); |
| 158 | + await expect( |
| 159 | + Promise.reject(original).catch(catchAndWrapError.withMessage('custom prefix')) |
| 160 | + ).rejects.toMatchObject({ |
| 161 | + message: 'custom prefix: downstream failure', |
| 162 | + }); |
| 163 | + }); |
| 164 | + |
| 165 | + it('wraps Fleet Not Found errors in NotFoundError with prefix', async () => { |
| 166 | + const original = new AgentNotFoundError('not found'); |
| 167 | + await expect( |
| 168 | + Promise.reject(original).catch(catchAndWrapError.withMessage('get agent')) |
| 169 | + ).rejects.toBeInstanceOf(NotFoundError); |
| 170 | + }); |
| 171 | + }); |
| 172 | +}); |
0 commit comments