Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,6 @@ const AXIOS_LEGACY_CONSUMERS = [
'src/platform/packages/shared/kbn-kbn-client/**/*.{js,mjs,ts,tsx}',
'src/platform/packages/shared/kbn-mcp-dev-server/**/*.{js,mjs,ts,tsx}',
'src/platform/packages/shared/kbn-test-saml-auth/**/*.{js,mjs,ts,tsx}',
'src/platform/plugins/shared/workflows_management/server/connectors/workflows/**/*.{js,mjs,ts,tsx}',
'src/platform/test/api_integration/apis/telemetry/**/*.{js,mjs,ts,tsx}',
'x-pack/examples/alerting_example/server/rule_types/**/*.{js,mjs,ts,tsx}',
'x-pack/packages/kbn-synthetics-private-location/**/*.{js,mjs,ts,tsx}',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,48 +7,47 @@
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import type { AxiosError } from 'axios';
import { createServiceError } from './utils';

describe('Workflows Utils', () => {
describe('createServiceError', () => {
it('should create error with Axios error message', () => {
const axiosError = {
isAxiosError: true,
it('should create error with HTTP error message', () => {
const httpError = {
name: 'Error',
response: {
data: {
message: 'API Error: Invalid request',
},
},
message: 'Request failed',
} as AxiosError;
} as Error;

const result = createServiceError(axiosError, 'Operation failed');
const result = createServiceError(httpError, 'Operation failed');

expect(result.message).toBe('Operation failed. Error: API Error: Invalid request');
});

it('should use Axios error message when response data has no message', () => {
const axiosError = {
isAxiosError: true,
it('should use HTTP error message when response data has no message', () => {
const httpError = {
name: 'Error',
response: {
data: {},
},
message: 'Network error',
} as AxiosError;
} as Error;

const result = createServiceError(axiosError, 'Operation failed');
const result = createServiceError(httpError, 'Operation failed');

expect(result.message).toBe('Operation failed. Error: Network error');
});

it('should handle Axios error without response data', () => {
const axiosError = {
isAxiosError: true,
it('should handle HTTP error without response data', () => {
const httpError = {
name: 'Error',
message: 'Connection timeout',
} as AxiosError;
} as Error;

const result = createServiceError(axiosError, 'Operation failed');
const result = createServiceError(httpError, 'Operation failed');

expect(result.message).toBe('Operation failed. Error: Connection timeout');
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,10 @@
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import { isAxiosError } from 'axios';

export const createServiceError = (error: Error, message: string) => {
if (isAxiosError(error)) {
const responseData = error.response?.data;
const errorMessage = responseData?.message || error.message;
return new Error(`${message}. Error: ${errorMessage}`);
}

return new Error(`${message}. Error: ${error.message}`);
// Some HTTP clients attach the parsed response body on `error.response.data`;
// prefer the upstream `message` field if it is present.
const responseData = (error as { response?: { data?: { message?: string } } }).response?.data;
const errorMessage = responseData?.message || error.message;
return new Error(`${message}. Error: ${errorMessage}`);
};
Loading