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

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

Open
Klanarm opened this issue Jul 22, 2024 · 5 comments
Labels
bug Something isn't working

Comments

@Klanarm
Copy link

Klanarm commented Jul 22, 2024

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

@Klanarm Klanarm added the bug Something isn't working label Jul 22, 2024
@SaltyAom
Copy link
Member

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
Copy link
Author

Klanarm commented Jul 24, 2024

it happening when add @elysiajs/html

@Klanarm
Copy link
Author

Klanarm commented Jul 24, 2024

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
Copy link

ghost commented Aug 28, 2024

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

@keepri
Copy link

keepri commented Sep 3, 2024

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.

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

No branches or pull requests

3 participants