Skip to content

Commit

Permalink
[FEAT] Criando novo erro para "to many requests" (#273)
Browse files Browse the repository at this point in the history
* feat(lambda): ✨ add custom error class for 'Too Many Requests' error

* refactor(lambda): ✨ update LambdaFunctionTypes to use a type alias
  • Loading branch information
Viserion77 authored Jun 14, 2024
1 parent b4b22c3 commit 30cd1f7
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 10 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "adapcon-utils-js",
"version": "1.4.4",
"version": "1.4.5",
"description": "Utils library for Javascript",
"keywords": [],
"author": {
Expand Down
8 changes: 7 additions & 1 deletion src/error/customErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,18 @@ class PreconditionFailedError extends CustomError {
kind = 'Precondition Failed Error'
}

class tooManyRequestsError extends CustomError {
statusCode = HttpStatuses.tooManyRequests
kind = 'Too Many Requests Error'
}

export {
BadRequestError,
CustomError,
IntegrationError,
InternalError,
NotFoundError,
UnauthorizedError,
PreconditionFailedError
PreconditionFailedError,
tooManyRequestsError
}
1 change: 1 addition & 0 deletions src/http/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export enum HttpStatuses {
preconditionFailed = 412,
teaPot = 418,
unprocessableEntity = 422,
tooManyRequests = 429,
internalError = 500,
notImplemented = 501
}
4 changes: 3 additions & 1 deletion src/lambda/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ export type DocfySettings = {
default?: any
}

export type LambdaFunctionTypes = 'screen' | 'integration' | 'public' | 'session' | 'hybrid'

export type Docfy = {
type: 'screen' | 'integration' | 'public' | 'session' | 'hybrid'
type: LambdaFunctionTypes
description: string
pathParameters?: Record<string, Omit<DocfySettings, 'required'>>
queryStringParameters?: Record<string, DocfySettings>
Expand Down
16 changes: 11 additions & 5 deletions src/lambda/lambdaResponses.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Context } from 'aws-lambda'
import { isNumber } from '../number'
import { objToStr } from '../object'
import type { Error, Headers } from './interfaces'
import type { Error, Headers, LambdaFunctionTypes } from './interfaces'

export const lambdaResp = (statusCode: number, body?: object | string, headers?: Headers) => ({
statusCode,
Expand All @@ -17,11 +18,16 @@ export const lambdaRespError = (err: Error) => {
return lambdaResp(500, (err.message) ? { error: err.message } : undefined)
}

type LambdaProcessErrorParams = {
type?: LambdaFunctionTypes
context: Context
err: Error
}
export const lambdaProcessError = <T>({
type = 'session', functionName, context, err
}) => {
console.error(`${type} ${functionName as string} - ERROR: `, err)
const metadata = { logStreamName: context?.logStreamName, functionName }
type = 'session', context, err
}: LambdaProcessErrorParams) => {
console.error(`${type} ${context.functionName} - ERROR: `, err)
const metadata = { logStreamName: context?.logStreamName, functionName: context.functionName }

if (err.statusCode) {
const errorResponse: {
Expand Down

0 comments on commit 30cd1f7

Please sign in to comment.