Last active
January 24, 2021 17:05
-
-
Save pawelgagorowski/4da6880752f7e1fe768da406037fd2bd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
import AWS from "aws-sdk"; | |
import { UserHeaderType } from "../../models/types"; | |
import DynamoDB from "../../classes/dynamoDB"; | |
import logger from "../../config/logger"; | |
const handler = async (event: HeaderRequestInterface<UserHeaderType>) => { | |
const genaralErrorMessage = "there was an error while checkig business name"; | |
try { | |
logger("info", event, "event"); | |
const missingBusinessErrorMessasge = "there is no business coresponding with the user while getting name of business"; | |
const noUserInHeaderErrorMessage = "there is no user in header while getting name of business"; | |
const { ['X-user']: user } = getHeaders<UserHeaderType>(event.headers, noUserInHeaderErrorMessage, "X-user"); | |
const params: AWS.DynamoDB.DocumentClient.GetItemInput = { | |
TableName: process.env.BUSINESS_TABLE, | |
Key: { | |
email: user | |
} | |
} | |
const business = await DynamoDB.getItem(params, missingBusinessErrorMessasge); | |
logger("info", business, "name of business"); | |
return business; | |
} catch(error) { | |
logger('error', error); | |
const response = error.customErrorMessage ? CustomError.createCustomErrorResponse(400, error.customErrorMessage) : CustomError.createCustomErrorResponse(400, genaralErrorMessage); | |
logger("error", response, "errorResponse"); | |
return response; | |
} | |
} | |
# to better show what I'm sending | |
class CustomError extends Error { | |
static createCustomErrorResponse(statusCode: number, errorMessage: string): ResponseType { | |
return { | |
statusCode, | |
body: JSON.stringify({errorMessage}), | |
} | |
} | |
static createCustomErrorMessage(customErrorMessage: string): CustomErrorMessageType { | |
return { | |
customErrorMessage | |
} | |
} | |
} | |
export { handler }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment