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
15 changes: 11 additions & 4 deletions packages/forest-cloud/src/services/http-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,30 @@ async function handledAxios<T>(
try {
return (await axios.default(axiosRequestConfig)).data;
} catch (e) {
const error: Error = e;
const error: Error & { code: string } = e;
let details = '';

if (error instanceof axios.AxiosError) {
const errors: { detail: string; status: number }[] = error.response?.data?.errors;
details = errors?.map(innerError => `🚨 ${innerError.detail}`).join(`\n`);
}

const loginAgainMessage =
" You can try to login again by running 'npx @forestadmin/forest-cloud@latest login'";

if (error.code === 'ERR_INVALID_CHAR' && error?.message.includes('"Authorization"')) {
throw new BusinessError(
`${error.message}\nYour authentication token seems incorrect.${loginAgainMessage}`,
);
}

if (e.response?.status === 400) {
throw new ValidationError(details);
} else {
const baseMessage = `${errorMessage}: ${error.message}\n${details}`.trim();

if (e.response?.status === 401 || e.response?.status === 403) {
const loginMessage =
" You can try to login again by running 'npx @forestadmin/forest-cloud@latest login'";
throw new BusinessError(`${baseMessage}\n${loginMessage}`);
throw new BusinessError(`${baseMessage}\n${loginAgainMessage}`);
}

throw new BusinessError(baseMessage);
Expand Down
14 changes: 14 additions & 0 deletions packages/forest-cloud/test/services/http-server.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable max-len */
import * as axios from 'axios';
import fs from 'fs';

Expand All @@ -24,6 +25,19 @@ describe('http-server', () => {
),
);
});

it('should provide specific detail if message contains ERR_INVALID_CHAR', async () => {
const error = new Error('Invalid character in header content ["Authorization"]');
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
error.code = 'ERR_INVALID_CHAR';
jest.mocked(axios.default).mockRejectedValue(error);
await expect(httpServer.getIntrospection()).rejects.toStrictEqual(
new BusinessError(
`Invalid character in header content ["Authorization"]\nYour authentication token seems incorrect. You can try to login again by running 'npx @forestadmin/forest-cloud@latest login'`,
),
);
});
});

describe('if it an axios error', () => {
Expand Down