Skip to content
Merged
9 changes: 8 additions & 1 deletion lib/errors/apps-sdk-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ export enum ERROR_CODE {
FORBIDDEN = 'forbidden',
NOT_FOUND = 'not found',
BAD_REQUEST = 'bad request',
INTERNAL_SERVER = 'internal server error'
INTERNAL_SERVER = 'internal server error',
TOO_MANY_REQUESTS = 'too many requests'
}

export class BaseError extends Error {
Expand Down Expand Up @@ -44,3 +45,9 @@ export class ForbiddenError extends BaseError {
super(ERROR_CODE.FORBIDDEN, message, StatusCodes.FORBIDDEN);
}
}

export class TooManyRequestsError extends BaseError {
constructor(public message: string) {
super(ERROR_CODE.TOO_MANY_REQUESTS, message, StatusCodes.TOO_MANY_REQUESTS);
}
}
6 changes: 5 additions & 1 deletion lib/secure-storage/secure-storage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BadRequestError, InternalServerError } from 'errors/apps-sdk-error';
import { BadRequestError, InternalServerError , TooManyRequestsError } from 'errors/apps-sdk-error';
import { getGcpConnectionData, getGcpIdentityToken } from 'lib/gcp/gcp';
import { MONDAY_CODE_RESERVED_PRIMITIVES_KEY } from 'lib/secure-storage/secure-storage.consts';
import { RequestOptions } from 'types/fetch';
Expand Down Expand Up @@ -45,6 +45,10 @@ const secureStorageFetch = async <T>(path: string, connectionData: ConnectionDat
try {
result = await fetchWrapper<VaultBaseResponse>(path, fetchObj);
} catch (error: unknown) {
if (error instanceof TooManyRequestsError) {
logger.warn('[secureStorageFetch] Rate limit exceeded while communicating with secure storage');
throw error;
}
logger.error('[secureStorageFetch] Unexpected error occurred while communicating with secure storage', { error: error as Error });
throw new InternalServerError('An issue occurred while accessing secure storage');
}
Expand Down
5 changes: 4 additions & 1 deletion lib/utils/fetch-wrapper.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { StatusCodes } from 'http-status-codes';
import fetch, { RequestInit, Response } from 'node-fetch';

import { ForbiddenError } from 'errors/apps-sdk-error';
import { ForbiddenError, TooManyRequestsError } from 'errors/apps-sdk-error';

const handleFetchErrors = (response: Response): void => {
if (response.status == StatusCodes.FORBIDDEN) {
throw new ForbiddenError('Forbidden action');
}
if (response.status == StatusCodes.TOO_MANY_REQUESTS) {
throw new TooManyRequestsError('request limit exceeded');
}
};

export async function fetchWrapper<TResponse>(
Expand Down
Loading