fix: better error handling within parseCookies (#3720)

This commit is contained in:
Patrik
2023-11-02 09:01:01 -04:00
committed by GitHub
parent 6325b334ec
commit 6b1b4ffd27

View File

@@ -1,5 +1,7 @@
import type { Request } from 'express' import type { Request } from 'express'
import { APIError } from '../errors'
export default function parseCookies(req: Request): { [key: string]: string } { export default function parseCookies(req: Request): { [key: string]: string } {
const list = {} const list = {}
const rc = req.headers.cookie const rc = req.headers.cookie
@@ -7,7 +9,15 @@ export default function parseCookies(req: Request): { [key: string]: string } {
if (rc) { if (rc) {
rc.split(';').forEach((cookie) => { rc.split(';').forEach((cookie) => {
const parts = cookie.split('=') const parts = cookie.split('=')
list[parts.shift().trim()] = decodeURI(parts.join('=')) const key = parts.shift().trim()
const encodedValue = parts.join('=')
try {
const decodedValue = decodeURI(encodedValue)
list[key] = decodedValue
} catch (e) {
throw new APIError(`Error decoding cookie value for key ${key}: ${e.message}`)
}
}) })
} }