0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-01-27 21:39:16 -05:00
logto/packages/core/src/middleware/koa-slonik-error-handler.test.ts
IceHe.xyz 4521c3c8d1
feat(core,schemas): save application id that the user first consented (#688)
* feat(core,schemas): save application id which the user first consented

* chore(core): fix grammatical mistake and typos
2022-05-05 06:57:20 +00:00

93 lines
2.5 KiB
TypeScript

import { Users } from '@logto/schemas';
import { NotFoundError, SlonikError } from 'slonik';
import RequestError from '@/errors/RequestError';
import { DeletionError, InsertionError, UpdateError } from '@/errors/SlonikError';
import { createContextWithRouteParameters } from '@/utils/test-utils';
import koaSlonikErrorHandler from './koa-slonik-error-handler';
describe('koaSlonikErrorHandler middleware', () => {
const next = jest.fn();
const ctx = createContextWithRouteParameters();
it('should throw no errors if no errors are catched', async () => {
await expect(koaSlonikErrorHandler()(ctx, next)).resolves.not.toThrow();
});
it('should throw original error if error type are not Slonik', async () => {
const error = new Error('foo');
next.mockImplementationOnce(() => {
throw error;
});
await expect(koaSlonikErrorHandler()(ctx, next)).rejects.toMatchError(error);
});
it('should throw original error if not intend to handled', async () => {
const error = new SlonikError();
next.mockImplementationOnce(() => {
throw error;
});
await expect(koaSlonikErrorHandler()(ctx, next)).rejects.toMatchError(error);
});
it('Insertion Error', async () => {
const error = new InsertionError(Users, { id: '123', applicationId: 'bar' });
next.mockImplementationOnce(() => {
throw error;
});
await expect(koaSlonikErrorHandler()(ctx, next)).rejects.toMatchError(
new RequestError({
code: 'entity.create_failed',
name: Users.tableSingular,
})
);
});
it('Update Error', async () => {
const error = new UpdateError(Users, { set: { name: 'punk' }, where: { id: '123' } });
next.mockImplementationOnce(() => {
throw error;
});
await expect(koaSlonikErrorHandler()(ctx, next)).rejects.toMatchError(
new RequestError({
code: 'entity.not_exists',
name: Users.tableSingular,
})
);
});
it('Deletion Error', async () => {
const error = new DeletionError();
next.mockImplementationOnce(() => {
throw error;
});
await expect(koaSlonikErrorHandler()(ctx, next)).rejects.toMatchError(
new RequestError({
code: 'entity.not_found',
status: 404,
})
);
});
it('NotFoundError', async () => {
const error = new NotFoundError();
next.mockImplementationOnce(() => {
throw error;
});
await expect(koaSlonikErrorHandler()(ctx, next)).rejects.toMatchError(
new RequestError({
code: 'entity.not_found',
status: 404,
})
);
});
});