elysia icon indicating copy to clipboard operation
elysia copied to clipboard

onError nomally i can return custom response but now return value dosent work

Open Klanarm opened this issue 1 year ago • 5 comments
trafficstars

What version of Elysia.JS is running?

[email protected]

What platform is your computer?

window x64

What steps can reproduce the bug?

image it should response back "VALIDATION" right ? but response back { "type": "validation", "on": "body", "summary": "Expected string length greater or equal to 7", "property": "/username", "message": "Expected string length greater or equal to 7", "expected": { "username": " ", "password": " " }, "found": { "username": "aa", "password": "12345678" }, "errors": [ { "summary": "Expected string length greater or equal to 7", "type": 52, "schema": { "maxLength": 12, "minLength": 7, "type": "string" }, "path": "/username", "value": "aa", "message": "Expected string length greater or equal to 7" } ] }

What is the expected behavior?

"VALIDATION"

What do you see instead?

{ "type": "validation", "on": "body", "summary": "Expected string length greater or equal to 7", "property": "/username", "message": "Expected string length greater or equal to 7", "expected": { "username": " ", "password": " " }, "found": { "username": "aa", "password": "12345678" }, "errors": [ { "summary": "Expected string length greater or equal to 7", "type": 52, "schema": { "maxLength": 12, "minLength": 7, "type": "string" }, "path": "/username", "value": "aa", "message": "Expected string length greater or equal to 7" } ] }

Additional information

i forks from my base-elysia git it work namorlly before update elysiajs because in package.json dependencies.elysia is latest

Klanarm avatar Jul 22 '24 09:07 Klanarm

Hi, I'm unable to reproduce the following code on Elysia 1.1.3 (latest)

new Elysia()
  .onError(({ code, error }) => {
	if(code === "VALIDATION")
		return code
  })
  .get('/query', () => {
	return 'a'
  }, {
	query: t.Object({
		a: t.String()
	})
  })
  .listen(3000)

Can you help provide an example code that may cause this problem?

SaltyAom avatar Jul 23 '24 14:07 SaltyAom

it happening when add @elysiajs/html

Klanarm avatar Jul 24 '24 06:07 Klanarm

import { Elysia, t } from "elysia";
import { html } from "@elysiajs/html";

const app = new Elysia().use(html()).onError(({ code, error }) => {
  if (code === "VALIDATION") {
    return code;
  }
});

app.get("/", ({ query }) => "hello", { query: t.Object({ a: t.String() }) });

app.listen(3000);

Klanarm avatar Jul 24 '24 08:07 Klanarm

i have same issue with elysia-msgpack plugin, dunno related to plugin or elysia

ghost avatar Aug 28 '24 00:08 ghost

it happening when add @elysiajs/html

Same here. If I access an endpoint that does not have the elysia/html plugin initialized I get a response back from .onError(), but if it is I do not.

Versions:

  • elysia: 1.1.8
  • elysia/html: 1.0.2

To reproduce:

const app = new Elysia()
    .onError(() => "hi, mark!")
    .get("/response", () => {
        throw new Error();
    })
    .use(html())
    .get("/no-response", () => {
        throw new Error();
    });

app.listen({});

LE: If I return "<h1>Hi, Mark!</h1>"; from .onError() I get a response back from the "/no-response" endpoint so I guess there's something to do with the fact that an HTML tag exists in the response. LLE: If I set autoDetect: false when initializing elysia/html I get a response back, but I am obligated to use ctx.html() when returning a page.

keepri avatar Sep 03 '24 18:09 keepri

so far I tried these in my onError and get these results:

set.status = 404
return resultTxt

returns content body correctly but with status=200

set.status = 404
throw new NotFoundError(resultTxt)

status and content are ok but content-type=text/plain

return new Response(resultTxt, {
  status: 404,
 headers: {
    'Content-Type': 'text/html'
  }
})

status =404 but a default NOT_FOUND text is returned as response body

any help is appreciated, I'm using v1.2.6

mohas avatar Dec 31 '24 15:12 mohas

Same issue here when using elsyia/html

awilderink avatar Jan 13 '25 10:01 awilderink