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
6 changes: 3 additions & 3 deletions packages/event-handler/src/rest/Router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ import {
} from './converters.js';
import { ErrorHandlerRegistry } from './ErrorHandlerRegistry.js';
import {
HttpError,
InternalServerError,
MethodNotAllowedError,
NotFoundError,
ServiceError,
} from './errors.js';
import { Route } from './Route.js';
import { RouteHandlerRegistry } from './RouteHandlerRegistry.js';
Expand Down Expand Up @@ -399,14 +399,14 @@ class Router {
headers: { 'Content-Type': 'application/json' },
});
} catch (handlerError) {
if (handlerError instanceof ServiceError) {
if (handlerError instanceof HttpError) {
return await this.handleError(handlerError, options);
}
return this.#defaultErrorHandler(handlerError as Error);
}
}

if (error instanceof ServiceError) {
if (error instanceof HttpError) {
return new Response(JSON.stringify(error.toJSON()), {
status: error.statusCode,
headers: { 'Content-Type': 'application/json' },
Expand Down
24 changes: 12 additions & 12 deletions packages/event-handler/src/rest/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ParameterValidationError extends RouteMatchingError {
}
}

abstract class ServiceError extends Error {
abstract class HttpError extends Error {
abstract readonly statusCode: HttpStatusCode;
abstract readonly errorType: string;
public readonly details?: Record<string, unknown>;
Expand All @@ -31,7 +31,7 @@ abstract class ServiceError extends Error {
details?: Record<string, unknown>
) {
super(message, options);
this.name = 'ServiceError';
this.name = 'HttpError';
this.details = details;
}

Expand All @@ -47,7 +47,7 @@ abstract class ServiceError extends Error {
}
}

class BadRequestError extends ServiceError {
class BadRequestError extends HttpError {
readonly statusCode = HttpStatusCodes.BAD_REQUEST;
readonly errorType = 'BadRequestError';

Expand All @@ -61,7 +61,7 @@ class BadRequestError extends ServiceError {
}
}

class UnauthorizedError extends ServiceError {
class UnauthorizedError extends HttpError {
readonly statusCode = HttpStatusCodes.UNAUTHORIZED;
readonly errorType = 'UnauthorizedError';

Expand All @@ -75,7 +75,7 @@ class UnauthorizedError extends ServiceError {
}
}

class ForbiddenError extends ServiceError {
class ForbiddenError extends HttpError {
readonly statusCode = HttpStatusCodes.FORBIDDEN;
readonly errorType = 'ForbiddenError';

Expand All @@ -89,7 +89,7 @@ class ForbiddenError extends ServiceError {
}
}

class NotFoundError extends ServiceError {
class NotFoundError extends HttpError {
readonly statusCode = HttpStatusCodes.NOT_FOUND;
readonly errorType = 'NotFoundError';

Expand All @@ -103,7 +103,7 @@ class NotFoundError extends ServiceError {
}
}

class MethodNotAllowedError extends ServiceError {
class MethodNotAllowedError extends HttpError {
readonly statusCode = HttpStatusCodes.METHOD_NOT_ALLOWED;
readonly errorType = 'MethodNotAllowedError';

Expand All @@ -117,7 +117,7 @@ class MethodNotAllowedError extends ServiceError {
}
}

class RequestTimeoutError extends ServiceError {
class RequestTimeoutError extends HttpError {
readonly statusCode = HttpStatusCodes.REQUEST_TIMEOUT;
readonly errorType = 'RequestTimeoutError';

Expand All @@ -131,7 +131,7 @@ class RequestTimeoutError extends ServiceError {
}
}

class RequestEntityTooLargeError extends ServiceError {
class RequestEntityTooLargeError extends HttpError {
readonly statusCode = HttpStatusCodes.REQUEST_ENTITY_TOO_LARGE;
readonly errorType = 'RequestEntityTooLargeError';

Expand All @@ -145,7 +145,7 @@ class RequestEntityTooLargeError extends ServiceError {
}
}

class InternalServerError extends ServiceError {
class InternalServerError extends HttpError {
readonly statusCode = HttpStatusCodes.INTERNAL_SERVER_ERROR;
readonly errorType = 'InternalServerError';

Expand All @@ -159,7 +159,7 @@ class InternalServerError extends ServiceError {
}
}

class ServiceUnavailableError extends ServiceError {
class ServiceUnavailableError extends HttpError {
readonly statusCode = HttpStatusCodes.SERVICE_UNAVAILABLE;
readonly errorType = 'ServiceUnavailableError';

Expand All @@ -183,7 +183,7 @@ export {
RequestEntityTooLargeError,
RequestTimeoutError,
RouteMatchingError,
ServiceError,
HttpError,
ServiceUnavailableError,
UnauthorizedError,
};
2 changes: 1 addition & 1 deletion packages/event-handler/src/rest/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ export {
export {
BadRequestError,
ForbiddenError,
HttpError,
InternalServerError,
MethodNotAllowedError,
NotFoundError,
ParameterValidationError,
RequestEntityTooLargeError,
RequestTimeoutError,
RouteMatchingError,
ServiceError,
ServiceUnavailableError,
UnauthorizedError,
} from './errors.js';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ describe('Class: Router - Error Handling', () => {
});
});

it('uses ServiceError toJSON method when no custom handler is registered', async () => {
it('uses HttpError toJSON method when no custom handler is registered', async () => {
// Prepare
const app = new Router();

Expand Down