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
57 changes: 57 additions & 0 deletions packages/agent-toolkit/src/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// eslint-disable-next-line max-classes-per-file
export class BusinessError extends Error {
// INTERNAL USAGES
public readonly isBusinessError = true;
public baseBusinessErrorName: string;

public readonly data: Record<string, unknown> | undefined;

constructor(message?: string, data?: Record<string, unknown>, name?: string) {
super(message);
this.name = name ?? this.constructor.name;
this.data = data;
}

/**
* We cannot rely on `instanceof` because there can be some mismatch between
* packages versions as dependencies of different packages.
* So this function is a workaround to check if an error is of a specific type.
*/
static isOfType(error: Error, ErrorConstructor: new (...args: never[]) => Error): boolean {
return (
error.name === ErrorConstructor.name ||
(error as BusinessError).baseBusinessErrorName === ErrorConstructor.name
);
}
}

export class ValidationError extends BusinessError {
constructor(message?: string, data?: Record<string, unknown>, name?: string) {
super(message, data, name);
this.baseBusinessErrorName = 'ValidationError';
}
}
export class BadRequestError extends BusinessError {
constructor(message?: string, data?: Record<string, unknown>, name?: string) {
super(message, data, name);
this.baseBusinessErrorName = 'BadRequestError';
}
}
export class UnprocessableError extends BusinessError {
constructor(message?: string, data?: Record<string, unknown>, name?: string) {
super(message, data, name);
this.baseBusinessErrorName = 'UnprocessableError';
}
}
export class ForbiddenError extends BusinessError {
constructor(message?: string, data?: Record<string, unknown>, name?: string) {
super(message, data, name);
this.baseBusinessErrorName = 'ForbiddenError';
}
}
export class NotFoundError extends BusinessError {
constructor(message?: string, data?: Record<string, unknown>, name?: string) {
super(message, data, name);
this.baseBusinessErrorName = 'NotFoundError';
}
}
1 change: 1 addition & 0 deletions packages/agent-toolkit/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './errors';
export type { AiProviderDefinition, AiProviderMeta, AiRouter } from './interfaces/ai';
76 changes: 76 additions & 0 deletions packages/agent-toolkit/test/errors.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/* eslint-disable max-classes-per-file */
import {
BadRequestError,
BusinessError,
ForbiddenError,
NotFoundError,
UnprocessableError,
ValidationError,
} from '../src/errors';

describe('errors', () => {
describe('BusinessError', () => {
describe('name', () => {
it('should have a name', () => {
const error = new BusinessError('test');
expect(error.name).toEqual('BusinessError');
});

it('should name the error after its constructor name', () => {
class CustomError extends BusinessError {}
const error = new CustomError('test');
expect(error.name).toEqual('CustomError');
});

it('should use the name when it is given', () => {
const error = new BusinessError('test', {}, 'MyName');
expect(error.name).toEqual('MyName');
});
});

describe('message', () => {
it('should have a message', () => {
const error = new BusinessError('test');
expect(error.message).toEqual('test');
});
});

describe('data', () => {
it('should have data', () => {
const error = new BusinessError('test', { foo: 'bar' });
expect(error.data).toEqual({ foo: 'bar' });
});

it('should set the data to undefined by default', () => {
const error = new BusinessError('test');
expect(error.data).toBeUndefined();
});
});

describe('isOfType', () => {
it('should return true when the error is of the given type', () => {
const error = new BusinessError('test');
expect(BusinessError.isOfType(error, BusinessError)).toBeTruthy();
});

it('should return false when the error is not of the given type', () => {
const error = new Error('test');
expect(BusinessError.isOfType(error, BusinessError)).toBeFalsy();
});
});
});

describe('baseBusinessErrorName', () => {
it.each([
{ ErrorClass: ValidationError, errorName: 'ValidationError' },
{ ErrorClass: BadRequestError, errorName: 'BadRequestError' },
{ ErrorClass: UnprocessableError, errorName: 'UnprocessableError' },
{ ErrorClass: ForbiddenError, errorName: 'ForbiddenError' },
{ ErrorClass: NotFoundError, errorName: 'NotFoundError' },
])('$errorName should have the correct baseBusinessErrorName', ({ ErrorClass, errorName }) => {
const error = new ErrorClass('test');
expect(error.baseBusinessErrorName).toEqual(errorName);
expect(error.isBusinessError).toBe(true);
});
});
});
1 change: 1 addition & 0 deletions packages/datasource-toolkit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"@types/uuid": "^10.0.0"
},
"dependencies": {
"@forestadmin/agent-toolkit": "1.0.0",
"luxon": "^3.2.1",
"object-hash": "^3.0.0",
"uuid": "11.0.2"
Expand Down
69 changes: 12 additions & 57 deletions packages/datasource-toolkit/src/errors.ts
Original file line number Diff line number Diff line change
@@ -1,60 +1,15 @@
// eslint-disable-next-line max-classes-per-file
export class BusinessError extends Error {
// INTERNAL USAGES
public readonly isBusinessError = true;
public baseBusinessErrorName: string;

public readonly data: Record<string, unknown> | undefined;

constructor(message?: string, data?: Record<string, unknown>, name?: string) {
super(message);
this.name = name ?? this.constructor.name;
this.data = data;
}

/**
* We cannot rely on `instanceof` because there can be some mismatch between
* packages versions as dependencies of different packages.
* So this function is a workaround to check if an error is of a specific type.
*/
static isOfType(error: Error, ErrorConstructor: new (...args: never[]) => Error): boolean {
return (
error.name === ErrorConstructor.name ||
(error as BusinessError).baseBusinessErrorName === ErrorConstructor.name
);
}
}

export class ValidationError extends BusinessError {
constructor(message?: string, data?: Record<string, unknown>, name?: string) {
super(message, data, name);
this.baseBusinessErrorName = 'ValidationError';
}
}
export class BadRequestError extends BusinessError {
constructor(message?: string, data?: Record<string, unknown>, name?: string) {
super(message, data, name);
this.baseBusinessErrorName = 'BadRequestError';
}
}
export class UnprocessableError extends BusinessError {
constructor(message?: string, data?: Record<string, unknown>, name?: string) {
super(message, data, name);
this.baseBusinessErrorName = 'UnprocessableError';
}
}
export class ForbiddenError extends BusinessError {
constructor(message?: string, data?: Record<string, unknown>, name?: string) {
super(message, data, name);
this.baseBusinessErrorName = 'ForbiddenError';
}
}
export class NotFoundError extends BusinessError {
constructor(message?: string, data?: Record<string, unknown>, name?: string) {
super(message, data, name);
this.baseBusinessErrorName = 'NotFoundError';
}
}
/* eslint-disable max-classes-per-file */
import { BusinessError, ValidationError } from '@forestadmin/agent-toolkit';

// Re-export base errors from agent-toolkit for backward compatibility
export {
BusinessError,
ValidationError,
BadRequestError,
UnprocessableError,
ForbiddenError,
NotFoundError,
} from '@forestadmin/agent-toolkit';

export class IntrospectionFormatError extends BusinessError {
constructor(sourcePackageName: '@forestadmin/datasource-sql' | '@forestadmin/datasource-mongo') {
Expand Down
54 changes: 1 addition & 53 deletions packages/datasource-toolkit/test/errors.test.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,6 @@
/* eslint-disable max-classes-per-file */
import { BusinessError, IntrospectionFormatError } from '../src/errors';
import { IntrospectionFormatError } from '../src/errors';

describe('errors', () => {
describe('BusinessError', () => {
describe('name', () => {
it('should have a name', () => {
const error = new BusinessError('test');
expect(error.name).toEqual('BusinessError');
});

it('should name the error after its constructor name', () => {
class CustomError extends BusinessError {}
const error = new CustomError('test');
expect(error.name).toEqual('CustomError');
});

it('should use the name when it is given', () => {
const error = new BusinessError('test', {}, 'MyName');
expect(error.name).toEqual('MyName');
});
});

describe('message', () => {
it('should have a message', () => {
const error = new BusinessError('test');
expect(error.message).toEqual('test');
});
});

describe('data', () => {
it('should have data', () => {
const error = new BusinessError('test', { foo: 'bar' });
expect(error.data).toEqual({ foo: 'bar' });
});

it('should set the data to undefined by default', () => {
const error = new BusinessError('test');
expect(error.data).toBeUndefined();
});
});

describe('isOfType', () => {
it('should return true when the error is of the given type', () => {
const error = new BusinessError('test');
expect(BusinessError.isOfType(error, BusinessError)).toBeTruthy();
});

it('should return false when the error is not of the given type', () => {
const error = new Error('test');
expect(BusinessError.isOfType(error, BusinessError)).toBeFalsy();
});
});
});

describe('IntrospectionFormatError', () => {
describe('message', () => {
it.each([['@forestadmin/datasource-mongo'], ['@forestadmin/datasource-sql']])(
Expand Down
Loading