mirror of
https://github.com/logto-io/logto.git
synced 2025-01-06 20:40:08 -05:00
parent
34e540d3ed
commit
108705076f
2 changed files with 88 additions and 0 deletions
76
packages/core/src/errors/RequestError/request-error.test.ts
Normal file
76
packages/core/src/errors/RequestError/request-error.test.ts
Normal file
|
@ -0,0 +1,76 @@
|
|||
import { LogtoErrorI18nKey } from '@logto/phrases';
|
||||
import i18next from 'i18next';
|
||||
|
||||
import initI18n from '@/i18n/init';
|
||||
|
||||
import RequestError from '.';
|
||||
|
||||
describe('RequestError', () => {
|
||||
beforeAll(async () => {
|
||||
await initI18n();
|
||||
});
|
||||
|
||||
it('standard RequestError use LogtoErrorCode', () => {
|
||||
const errorCode = 'auth.unauthorized';
|
||||
const expectMessage = i18next.t<string, LogtoErrorI18nKey>(`errors:${errorCode}`);
|
||||
const data = { foo: 'foo' };
|
||||
|
||||
const newRequestError = new RequestError(errorCode, data);
|
||||
|
||||
expect(newRequestError.status).toEqual(400);
|
||||
expect(newRequestError.code).toEqual(errorCode);
|
||||
expect(newRequestError.expose).toEqual(true);
|
||||
expect(newRequestError.message).toEqual(expectMessage);
|
||||
expect(newRequestError.body).toEqual({
|
||||
code: errorCode,
|
||||
data,
|
||||
message: expectMessage,
|
||||
});
|
||||
});
|
||||
|
||||
it('standard RequestError use RequestErrorMetadata', () => {
|
||||
const errorCode = 'auth.unauthorized';
|
||||
const expectMessage = i18next.t<string, LogtoErrorI18nKey>(`errors:${errorCode}`);
|
||||
const data = { foo: 'foo' };
|
||||
|
||||
const newRequestError = new RequestError(
|
||||
{
|
||||
code: errorCode,
|
||||
status: 500,
|
||||
expose: false,
|
||||
},
|
||||
data
|
||||
);
|
||||
|
||||
expect(newRequestError.status).toEqual(500);
|
||||
expect(newRequestError.code).toEqual(errorCode);
|
||||
expect(newRequestError.expose).toEqual(false);
|
||||
expect(newRequestError.message).toEqual(expectMessage);
|
||||
expect(newRequestError.body).toEqual({
|
||||
code: errorCode,
|
||||
data,
|
||||
message: expectMessage,
|
||||
});
|
||||
});
|
||||
|
||||
it('RequestError with interpolation error message', () => {
|
||||
const errorCode = 'entity.create_failed';
|
||||
const entityName = 'mockEntity';
|
||||
const expectMessage = i18next.t<string, LogtoErrorI18nKey>(`errors:${errorCode}`, {
|
||||
name: entityName,
|
||||
});
|
||||
|
||||
const newRequestError = new RequestError({
|
||||
code: errorCode,
|
||||
status: 500,
|
||||
expose: false,
|
||||
name: entityName,
|
||||
});
|
||||
|
||||
expect(newRequestError.status).toEqual(500);
|
||||
expect(newRequestError.code).toEqual(errorCode);
|
||||
expect(newRequestError.expose).toEqual(false);
|
||||
expect(newRequestError.message).toEqual(expectMessage);
|
||||
expect(newRequestError.message).toContain('mockEntity');
|
||||
});
|
||||
});
|
12
packages/core/src/errors/SlonikError/slonik-error.test.ts
Normal file
12
packages/core/src/errors/SlonikError/slonik-error.test.ts
Normal file
|
@ -0,0 +1,12 @@
|
|||
import { SlonikError } from 'slonik';
|
||||
|
||||
import { DeletionError } from '.';
|
||||
|
||||
describe('SlonikError', () => {
|
||||
it('DeletionError', () => {
|
||||
const error = new DeletionError('user', 'foo');
|
||||
expect(error instanceof SlonikError).toEqual(true);
|
||||
expect(error.table).toEqual('user');
|
||||
expect(error.id).toEqual('foo');
|
||||
});
|
||||
});
|
Loading…
Reference in a new issue