Skip to content
Myr

fix(validator): Invalid HTTP header error is hidden (#1694)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* Invalid HTTP header error is hidden This error is captured and never shown which may by misleading. This PR suggest to throw this error directly to the reponse. * fixed the linter error * add a test * denoify ---------

authored by Robert Sasak · committed by GitHub · parent f20e4fc · Browse files · 3 files+36 added−16 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Yusuke Wada <yusuke@kamawada.com>

Diff stats

3 files+36 added−16 removed

  • tests:+4 added−0 removed
  • other:+32 added−16 removed

3 files+36 added−16 removed

modified:deno_dist/validator/validator.ts+16 added-8 removed
modified:src/validator/validator.test.ts+4 added
115 unmodified lines
116 any: 'thing',117 }),118 })119 expect(res.status).toBe(400)120 expect(await res.json()).toEqual({121 success: false,122 message: 'Invalid HTTP header: Content-Type=Xapplication/json',123 })124 })125})126127describe('Malformed FormData request', () => {
697 unmodified lines
modified:src/validator/validator.ts+16 added-8 removed
44 unmodified lines
45 >46): MiddlewareHandler<E, P, V> => {47 return async (c, next) => {48 let value = {}49 const contentType = c.req.header('Content-Type')5051 switch (target) {52 case 'json':53 if (!contentType || !contentType.startsWith('application/json')) {54 const message = `Invalid HTTP header: Content-Type=${contentType}`55 console.error(message)56 return c.json(57 {58 success: false,59 message,60 },61 40062 )63 }64 /**65 * Get the arrayBuffer first, create JSON object via Response,66 * and cache the arrayBuffer in the c.req.bodyCache.67 */68 try {53 const contentType = c.req.header('Content-Type')54 if (!contentType || !contentType.startsWith('application/json')) {55 throw new Error(`Invalid HTTP header: Content-Type=${contentType}`)56 }57 /**58 * Get the arrayBuffer first, create JSON object via Response,59 * and cache the arrayBuffer in the c.req.bodyCache.60 */69 const arrayBuffer = c.req.bodyCache.arrayBuffer ?? (await c.req.raw.arrayBuffer())70 value = await new Response(arrayBuffer).json()71 c.req.bodyCache.json = value72 c.req.bodyCache.arrayBuffer = arrayBuffer
71 unmodified lines