Skip to content

Instantly share code, notes, and snippets.

@pawelgagorowski
Last active January 24, 2021 17:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pawelgagorowski/4da6880752f7e1fe768da406037fd2bd to your computer and use it in GitHub Desktop.
Save pawelgagorowski/4da6880752f7e1fe768da406037fd2bd to your computer and use it in GitHub Desktop.
'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