Skip to content

Commit

Permalink
CAS-12599 Add handling API rate limiting and general errors
Browse files Browse the repository at this point in the history
  • Loading branch information
megawebmaster committed Jul 4, 2024
1 parent 9319c87 commit 61260e9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Changelog

## Unreleased

- [#164](https://github.com/castle/castle-node/pull/164)
* handle rate limiting and general API errors
- [#159](https://github.com/castle/castle-node/pull/159)
* fix failed event status name
- [#160](https://github.com/castle/castle-node/pull/160) [#161](https://github.com/castle/castle-node/pull/161) [#162](https://github.com/castle/castle-node/pull/162) [#163](https://github.com/castle/castle-node/pull/163)
* Bump dependencies

## 2.2.1

- [#157](https://github.com/castle/castle-node/pull/157)
Expand Down
8 changes: 6 additions & 2 deletions src/core/services/core-process-response.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
InvalidRequestTokenError,
InternalServerError,
APIError,
RateLimitError,
} from '../../errors';
import { LoggerService } from '../../logger/logger.module';

Expand All @@ -18,6 +19,7 @@ const RESPONSE_ERRORS = {
'404': NotFoundError,
'419': UserUnauthorizedError,
'422': InvalidParametersError,
'429': RateLimitError,
};

const RESPONSE_SUB_ERRORS = {
Expand Down Expand Up @@ -65,10 +67,12 @@ export const CoreProcessResponseService = {

// Throw a special exception for subtype errors if defined. Eg. for
// invalid request token, which is a subtype of InvalidParametersError.
// Otherwise, throw exception as defined per status code
// Otherwise, throw exception as defined per status code or a general
// API error
const err =
RESPONSE_SUB_ERRORS[body.type] ||
RESPONSE_ERRORS[response.status.toString()];
RESPONSE_ERRORS[response.status.toString()] ||
APIError;

throw new err(`Castle: Responded with ${response.status} code`);
},
Expand Down
8 changes: 8 additions & 0 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ export class InvalidRequestTokenError extends InvalidParametersError {
}
}

// api error rate limited 429
export class RateLimitError extends APIError {
constructor(message: string) {
super(message);
this.name = 'RateLimitError';
}
}

// all internal server errors
export class InternalServerError extends APIError {
constructor(message: string) {
Expand Down

0 comments on commit 61260e9

Please sign in to comment.