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

no catch case in case DELETE request has no body #495

Closed
5 tasks
huilensolis opened this issue Feb 21, 2024 · 4 comments
Closed
5 tasks

no catch case in case DELETE request has no body #495

huilensolis opened this issue Feb 21, 2024 · 4 comments
Labels
bug Something isn't working pending Should be resolved in next release

Comments

@huilensolis
Copy link

What version of Elysia.JS is running?

0.8.17

What platform is your computer?

Linux 6.5.5-arch1-1 x86_64 unknown

What steps can reproduce the bug?

  • Set up an Elysia JS server
  • Set up a .onError method on your elysia app
YourElysiaApp.onError(({ code, error, path, set }) => {
  console.log({ path, error, code });
})
  • Set up a route with a DELETE method
  • Create a request with the fetch API on bun with DELETE method and headers content type application/json
    const res = await app.handle(
      new Request(`${endpointPath}/`, {
        method: "DELETE",
        headers: { "Content-Type": "application/json" }
      }),
    );
  • check the console

What is the expected behavior?

don't throw an error if the request has no body, or provide a descriptive error message letting the developer know that the error is being thrown because he has setten 'Content-type': 'aplication/json' but there was an error parsing the body.
or explain that the request body is going to be parsed always.

What do you see instead?

{
  path: "/user/",
  error: 61 |                                   const index = contentType.indexOf(';')
62 |                                    if (index !== -1) contentType = contentType.substring(0, index)
63 |
64 |                            switch (contentType) {
65 |                                    case 'application/json':
66 |                                            c.body = await c.request.json()
                          ^
SyntaxError: Unexpected end of JSON input
,
  code: "UNKNOWN",
}

Additional information

No response

@huilensolis huilensolis added the bug Something isn't working label Feb 21, 2024
@SaltyAom
Copy link
Member

Fixed in 711925a under 1.0.0-beta.14.

If resolved, please leave the issue opened until release on next stable release.

@SaltyAom SaltyAom added the pending Should be resolved in next release label Feb 26, 2024
@msanchezdev
Copy link

msanchezdev commented Mar 8, 2024

@SaltyAom when the body is extracted or accessed the following error is thrown:

Error: Failed to parse body as found: ''

It no longer erros with unexpected end of json input but still throwing when trying to parse the body on an onBeforeHandle or onTransform, haven't tried other cases. Been trying to make a log for all requests:

  .onRequest(async ({ request }) => {
    console.log('< req.url =', request.url);
    console.log('< req.method =', request.method);
    console.log('< req.headers =', request.headers);
  })
  .onBeforeHandle(async ({ body }) => {
    console.log('< req.body =', body);
    console.log();
  })
  .onResponse(async ({ set }) => {
    console.log('> status =', set.status);
    console.log();
  })

I think we should have a default value for the body if possible (when type is explicitly set?), in cases where the body hasn't been parsed or cant be known (onTransform or onBeforeHandle which are my case)

PD: Out of the scope of this ticket but would be great to have access to the response body on an onResponse or onAfterHandle

@kravetsone
Copy link
Contributor

What version of Elysia.JS is running?

0.8.17

What platform is your computer?

Linux 6.5.5-arch1-1 x86_64 unknown

What steps can reproduce the bug?

  • Set up an Elysia JS server
  • Set up a .onError method on your elysia app
YourElysiaApp.onError(({ code, error, path, set }) => {
  console.log({ path, error, code });
})
  • Set up a route with a DELETE method
  • Create a request with the fetch API on bun with DELETE method and headers content type application/json
    const res = await app.handle(
      new Request(`${endpointPath}/`, {
        method: "DELETE",
        headers: { "Content-Type": "application/json" }
      }),
    );
  • check the console

What is the expected behavior?

don't throw an error if the request has no body, or provide a descriptive error message letting the developer know that the error is being thrown because he has setten 'Content-type': 'aplication/json' but there was an error parsing the body. or explain that the request body is going to be parsed always.

What do you see instead?

{
  path: "/user/",
  error: 61 |                                   const index = contentType.indexOf(';')
62 |                                    if (index !== -1) contentType = contentType.substring(0, index)
63 |
64 |                            switch (contentType) {
65 |                                    case 'application/json':
66 |                                            c.body = await c.request.json()
                          ^
SyntaxError: Unexpected end of JSON input
,
  code: "UNKNOWN",
}

Additional information

No response

Why would you lie to the server about the content-type?

@SaltyAom
Copy link
Member

Fixed with 1678c63.

Feel free to reopen the issue if the problem still persists.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working pending Should be resolved in next release
Projects
None yet
Development

No branches or pull requests

4 participants