@simpleapps-com/augur-api
Version:
TypeScript client library for Augur microservices API endpoints
94 lines • 2.91 kB
JavaScript
export class AugurError extends Error {
constructor(params) {
super(params.message);
this.name = 'AugurAPIError';
this.code = params.code;
this.statusCode = params.statusCode;
this.service = params.service;
this.endpoint = params.endpoint;
this.requestId = params.requestId;
this.validationErrors = params.validationErrors;
// Maintains proper stack trace for where our error was thrown (only available on V8)
if (Error.captureStackTrace) {
Error.captureStackTrace(this, AugurError);
}
}
}
export class ValidationError extends AugurError {
constructor(params) {
super({
...params,
code: 'VALIDATION_ERROR',
statusCode: 400,
});
}
/**
* Format validation errors into human-readable messages
* @returns Array of formatted error messages
*/
getFormattedErrors() {
if (!this.validationErrors) {
return [];
}
return this.validationErrors.map(error => {
const path = error.path.length > 0 ? error.path.join('.') : 'root';
return `${path}: ${error.message}`;
});
}
/**
* Get detailed validation error information for debugging
* @returns Object with detailed error information
*/
getDetailedErrors() {
if (!this.validationErrors) {
return [];
}
return this.validationErrors.map(error => ({
path: error.path.length > 0 ? error.path.join('.') : 'root',
message: error.message,
code: error.code,
received: 'received' in error ? error.received : undefined,
expected: 'expected' in error ? error.expected : undefined,
}));
}
/**
* Override toString to include formatted validation errors
* @returns String representation with validation details
*/
toString() {
const baseMessage = super.toString();
const formattedErrors = this.getFormattedErrors();
if (formattedErrors.length > 0) {
return `${baseMessage}\nValidation errors:\n${formattedErrors.map(err => ` - ${err}`).join('\n')}`;
}
return baseMessage;
}
}
export class AuthenticationError extends AugurError {
constructor(params) {
super({
...params,
code: 'AUTHENTICATION_ERROR',
statusCode: 401,
});
}
}
export class NotFoundError extends AugurError {
constructor(params) {
super({
...params,
code: 'NOT_FOUND',
statusCode: 404,
});
}
}
export class RateLimitError extends AugurError {
constructor(params) {
super({
...params,
code: 'RATE_LIMIT_EXCEEDED',
statusCode: 429,
});
}
}
//# sourceMappingURL=errors.js.map