elysia icon indicating copy to clipboard operation
elysia copied to clipboard

no catch case in case DELETE request has no body

Open huilensolis opened this issue 1 year ago • 3 comments

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 avatar Feb 21 '24 18:02 huilensolis

Fixed in 711925a under 1.0.0-beta.14.

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

SaltyAom avatar Feb 26 '24 07:02 SaltyAom

@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

msanchezdev avatar Mar 08 '24 00:03 msanchezdev

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?

kravetsone avatar Jul 21 '24 11:07 kravetsone

Fixed with 1678c63.

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

SaltyAom avatar Aug 30 '24 18:08 SaltyAom