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

Parsing malformed body with NODE_ENV 'production' results in UNKNOWN error #1005

Closed
siikasmaa opened this issue Jan 9, 2025 · 2 comments · May be fixed by #1006
Closed

Parsing malformed body with NODE_ENV 'production' results in UNKNOWN error #1005

siikasmaa opened this issue Jan 9, 2025 · 2 comments · May be fixed by #1006
Labels
bug Something isn't working

Comments

@siikasmaa
Copy link

What version of Elysia is running?

1.2.10

What platform is your computer?

Darwin 24.2.0 arm64 arm

What steps can reproduce the bug?

For the following Bun application:

import { Elysia, t } from 'elysia'

process.env.NODE_ENV = 'production'

const app = new Elysia()
  .onError(({ error, code }) => {
    console.log(error)
    return { code }
  })
  .post('/', ({ body }) => body, {
    body: t.Object({
      name: t.String()
    })
  })
  .listen(3000)

console.log(
  `🦊 Elysia is running at ${app.server?.hostname}:${app.server?.port}`
)

with the following request to trigger a validation error:

curl -X POST http://localhost:3000 -H 'content-type: application/json' -d '{"name": 1}'

What is the expected behavior?

Response should be:

{"code":"VALIDATION"}

What do you see instead?

Response is:

{"code":"UNKNOWN"}

and the following is logged:

191 |                   : 'Errors' in validator
192 |                           ? validator.Errors(value).First()
193 |                           : Value.Errors(validator, value).First()
194 | 
195 |           const customError =
196 |                   error.schema.message || error?.schema.error !== undefined
         ^
TypeError: undefined is not an object (evaluating 'error2.schema')

Additional information

Since Elysia version 1.2.3 the possibly undefined error is accessed without optional chain in the class ValidationError.

From src/error.ts:

const error = isProduction
			? undefined
			: 'Errors' in validator
				? validator.Errors(value).First()
				: Value.Errors(validator, value).First()

const customError =
	error.schema.message || error?.schema.error !== undefined
		? typeof error.schema.error === 'function'
			? error.schema.error({
					type,
					validator,
					value,
					get errors() {
						return [...validator.Errors(value)].map(
							mapValueError
						)
					}
				})
			: error.schema.error
		: undefined

The error object should be accessed safely as it can be undefined in production environment (NODE_ENV or ENV is production).

Have you try removing the node_modules and bun.lockb and try again yet?

Yes

@GautierT
Copy link

@SaltyAom
Copy link
Member

SaltyAom commented Feb 2, 2025

Should be fixed with caaf17c, published under 1.2.11

@SaltyAom SaltyAom closed this as completed Feb 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants