elysiajs / elysia

Ergonomic Framework for Humans
https://elysiajs.com
MIT License
10.27k stars 219 forks source link

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

Open Klanarm opened 2 months ago

Klanarm commented 2 months ago

What version of Elysia.JS is running?

elysia@1.1.3

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

SaltyAom commented 2 months ago

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?

Klanarm commented 2 months ago

it happening when add @elysiajs/html

Klanarm commented 2 months ago
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);
ghost commented 1 month ago

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

keepri commented 1 month ago

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:

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.