Skip to content

Commit

Permalink
fix: handle errors on body parser middleware (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
NikhilShahi authored Nov 1, 2022
1 parent ad39dc1 commit 926d60d
Showing 1 changed file with 70 additions and 56 deletions.
126 changes: 70 additions & 56 deletions backend/src/middleware/body-parser-middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import MIMEType from "whatwg-mimetype"
import { TraceParams } from "@common/types"
import { getDataType, parsedJsonNonNull } from "utils"
import { DataType } from "@common/enums"
import ApiResponseHandler from "api-response-handler"
import Error500InternalServer from "errors/error-500-internal-server"

const parseData = (data: string) => {
if (!data) {
Expand Down Expand Up @@ -47,73 +49,85 @@ const getParsedBodyString = (
}
}

const getMimeType = (contentType: string) => {
try {
return new MIMEType(contentType)
} catch (err) {
return new MIMEType("text/plain")
}
}

export const bodyParserMiddleware = async (
req: Request,
res: Response,
next: NextFunction,
) => {
let traceParamsList: TraceParams[] = req.body
const dataType = getDataType(req.body)
if (dataType !== DataType.ARRAY) {
traceParamsList = [req.body]
}
traceParamsList.forEach((traceParams, idx) => {
const requestHeaders = traceParams.request?.headers ?? []
const responseHeaders = traceParams.response?.headers ?? []
let requestBodyContentType = null
let responseBodyContentType = null
try {
let traceParamsList: TraceParams[] = req.body
const dataType = getDataType(req.body)
if (dataType !== DataType.ARRAY) {
traceParamsList = [req.body]
}
traceParamsList.forEach((traceParams, idx) => {
const requestHeaders = traceParams.request?.headers ?? []
const responseHeaders = traceParams.response?.headers ?? []
let requestBodyContentType = null
let responseBodyContentType = null

requestHeaders.forEach(header => {
if (header.name.toLowerCase() === "content-type") {
requestBodyContentType = header.value
}
})
responseHeaders.forEach(header => {
if (header.name.toLowerCase() === "content-type") {
responseBodyContentType = header.value
}
})
requestHeaders.forEach(header => {
if (header.name.toLowerCase() === "content-type") {
requestBodyContentType = header.value
}
})
responseHeaders.forEach(header => {
if (header.name.toLowerCase() === "content-type") {
responseBodyContentType = header.value
}
})

if (requestBodyContentType) {
const reqMimeType = new MIMEType(requestBodyContentType)
const parsedBodyString = getParsedBodyString(
traceParams?.request?.body,
reqMimeType?.subtype,
reqMimeType?.parameters,
)
if (dataType === DataType.ARRAY) {
req.body[idx].request.body = parsedBodyString
} else {
req.body.request.body = parsedBodyString
}
} else {
if (dataType === DataType.ARRAY) {
req.body[idx].request.body = traceParams?.request?.body
if (requestBodyContentType) {
const reqMimeType = getMimeType(requestBodyContentType)
const parsedBodyString = getParsedBodyString(
traceParams?.request?.body,
reqMimeType?.subtype,
reqMimeType?.parameters,
)
if (dataType === DataType.ARRAY) {
req.body[idx].request.body = parsedBodyString
} else {
req.body.request.body = parsedBodyString
}
} else {
req.body.request.body = traceParams?.request?.body
if (dataType === DataType.ARRAY) {
req.body[idx].request.body = traceParams?.request?.body
} else {
req.body.request.body = traceParams?.request?.body
}
}
}

if (responseBodyContentType) {
const resMimeType = new MIMEType(responseBodyContentType)
const parsedBodyString = getParsedBodyString(
traceParams?.response?.body,
resMimeType?.subtype,
resMimeType?.parameters,
)
if (dataType === DataType.ARRAY) {
req.body[idx].response.body = parsedBodyString
} else {
req.body.response.body = parsedBodyString
}
} else {
if (dataType === DataType.ARRAY) {
req.body[idx].response.body = traceParams?.response?.body
if (responseBodyContentType) {
const resMimeType = getMimeType(responseBodyContentType)
const parsedBodyString = getParsedBodyString(
traceParams?.response?.body,
resMimeType?.subtype,
resMimeType?.parameters,
)
if (dataType === DataType.ARRAY) {
req.body[idx].response.body = parsedBodyString
} else {
req.body.response.body = parsedBodyString
}
} else {
req.body.response.body = traceParams?.response?.body
if (dataType === DataType.ARRAY) {
req.body[idx].response.body = traceParams?.response?.body
} else {
req.body.response.body = traceParams?.response?.body
}
}
}
})
})

next()
next()
} catch (err) {
await ApiResponseHandler.error(res, new Error500InternalServer(err))
}
}

0 comments on commit 926d60d

Please sign in to comment.