Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

release #182

Merged
merged 4 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/api/src/functions/signin/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { generateToken } from "@libs/auth"
import { ObokuErrorCode } from "@oboku/shared"
import { createHttpError } from "@libs/httpErrors"
import { getParametersValue } from "@libs/ssm"
import { withMiddy } from "@libs/middy/withMiddy"

const firebaseConfig = JSON.parse(
Buffer.from(process.env.FIREBASE_CONFIG ?? "", "base64").toString() ?? "{}"
Expand Down
22 changes: 22 additions & 0 deletions packages/api/src/libs/middy/unexpectedErrorToHttpError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { createHttpError } from "@libs/httpErrors"
import { MiddlewareObj } from "@middy/core"
import { ObokuErrorCode } from "@oboku/shared"

export const unexpectedErrorToHttpError = (): MiddlewareObj => ({
onError: async (request) => {
if (request.error) {
console.error("error received", request.error)
}

if (request.error) {
request.error = createHttpError(500, {
code: ObokuErrorCode.UNKNOWN,
message: request.error.message
})

// eslint-disable-next-line no-extra-semi
;(request.error as any).expose =
(request.error as any)?.expose ?? process.env.NODE_ENV === "development"
}
}
})
24 changes: 2 additions & 22 deletions packages/api/src/libs/middy/withMiddy.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import middy from "@middy/core"
import { jsonSafeParse } from "@middy/util"
import middyJsonBodyParser from "@middy/http-json-body-parser"
import httpErrorHandler from "@middy/http-error-handler"
import httpHeaderNormalizer from "@middy/http-header-normalizer"
import cors from "@middy/http-cors"
import { transpileSchema } from "@middy/validator/transpile"
import validator from "@middy/validator"
import { createHttpError } from "@libs/httpErrors"
import { ObokuErrorCode } from "@oboku/shared"
import { unexpectedErrorToHttpError } from "./unexpectedErrorToHttpError"

export const withMiddy = (
handler: any,
Expand Down Expand Up @@ -68,25 +66,7 @@ export const withMiddy = (
fallbackMessage: `An error occurred`
})
)
.use({
onError: async (request) => {
if (request.error) {
console.error("error received", request.error)
}

if (request.error) {
request.error = createHttpError(500, {
code: ObokuErrorCode.UNKNOWN,
message: request.error.message
})

// eslint-disable-next-line no-extra-semi
;(request.error as any).expose =
(request.error as any)?.expose ??
process.env.NODE_ENV === "development"
}
}
})
.use(unexpectedErrorToHttpError())
// @todo eventually protect the api and only allow a subset of origins
.use(
withCors
Expand Down
Loading