Repository: elysiajs/elysia (TypeScript)

Issue:
When aot is false, error responses return a 500
### What version of Elysia is running?

1.2.4

### What platform is your computer?

Darwin 23.6.0 arm64 arm

### What steps can reproduce the bug?

When `aot` is false, error responses don't work.

```ts
new Elysia({ aot: false })
 .get('/', ({ error }) => {
    return error(400);
  })
```

### What is the expected behavior?

400 status code

### What do you see instead?

500 status code with message: error4 is not a function

### Additional information

_No response_

### Have you try removing the `node_modules` and `bun.lockb` and try again yet?

Yes


Discussion:
I’m having the same issue as well, elysiajs version `1.2.12`
I am also looking for an alternative way to write this as a temporary solution, which is why I used the following approach:

direct import error from elysia

```typescript
import { Elysia, error } from 'elysia'
new Elysia({
    aot: true
})
.derive(() => {
    return error(401)
})
.get('/', 'ok')
.listen(3000)
// http://127.0.0.1:3000 shows `Unauthorized`
```
Unfortunately, this alternative solution is still affected by the AOT flag. When AOT is set to false, the error returned in derive will be ignored. This is weird and counterintuitive.

```typescript
import { Elysia, error } from 'elysia'
new Elysia({
    aot: false
})
.derive(() => {
    return error(401)
})
.get('/', 'ok')
.listen(3000)
// http://127.0.0.1:3000 shows `ok`
```

but I have to turn aot off for the reason #943 😮‍💨

Fixed with elysia `1.2.13`



Patch:
diff --git a/src/dynamic-handle.ts b/src/dynamic-handle.ts
index 98c5ecc8..0782186f 100644
--- a/src/dynamic-handle.ts
+++ b/src/dynamic-handle.ts
@@ -3,6 +3,7 @@ import type { AnyElysia } from '.'
 import {
 	ElysiaCustomStatusResponse,
 	ElysiaErrors,
+	error,
 	NotFoundError,
 	ValidationError
 } from './error'
@@ -60,6 +61,7 @@ export const createDynamicHandler = (app: AnyElysia) => {
 				request,
 				path,
 				qi,
+				error,
 				redirect
 			}
 		) as unknown as Context & {


Test patch:
diff --git a/test/lifecycle/error.test.ts b/test/lifecycle/error.test.ts
index 80db91c7..7aaa04a9 100644
--- a/test/lifecycle/error.test.ts
+++ b/test/lifecycle/error.test.ts
@@ -120,43 +120,57 @@ describe('error', () => {
 		expect(response.status).toBe(500)
 	})
 
-	it('return correct number status on error function', async () => {
-		const app = new Elysia().get('/', ({ error }) =>
-			error(418, 'I am a teapot')
-		)
+	it.each([true, false])(
+		'return correct number status on error function with aot: %p',
+		async (aot) => {
+			const app = new Elysia({ aot }).get('/', ({ error }) =>
+				error(418, 'I am a teapot')
+			)
 
-		const response = await app.handle(req('/'))
+			const response = await app.handle(req('/'))
 
-		expect(response.status).toBe(418)
-	})
+			expect(response.status).toBe(418)
+		}
+	)
 
-	it('return correct named status on error function', async () => {
-		const app = new Elysia().get('/', ({ error }) =>
-			error("I'm a teapot", 'I am a teapot')
-		)
+	it.each([true, false])(
+		'return correct named status on error function with aot: %p',
+		async (aot) => {
+			const app = new Elysia({ aot }).get('/', ({ error }) =>
+				error("I'm a teapot", 'I am a teapot')
+			)
 
-		const response = await app.handle(req('/'))
+			const response = await app.handle(req('/'))
 
-		expect(response.status).toBe(418)
-	})
+			expect(response.status).toBe(418)
+		}
+	)
 
-	it('return correct number status without value on error function', async () => {
-		const app = new Elysia().get('/', ({ error }) => error(418))
+	it.each([true, false])(
+		'return correct number status without value on error function with aot: %p',
+		async (aot) => {
+			const app = new Elysia({ aot }).get('/', ({ error }) => error(418))
 
-		const response = await app.handle(req('/'))
+			const response = await app.handle(req('/'))
 
-		expect(response.status).toBe(418)
-		expect(await response.text()).toBe("I'm a teapot")
-	})
+			expect(response.status).toBe(418)
+			expect(await response.text()).toBe("I'm a teapot")
+		}
+	)
 
-	it('return correct named status without value on error function', async () => {
-		const app = new Elysia().get('/', ({ error }) => error("I'm a teapot"))
+	it.each([true, false])(
+		'return correct named status without value on error function with aot: %p',
+		async (aot) => {
+			const app = new Elysia({ aot }).get('/', ({ error }) =>
+				error("I'm a teapot")
+			)
 
-		const response = await app.handle(req('/'))
+			const response = await app.handle(req('/'))
 
-		expect(response.status).toBe(418)
-		expect(await response.text()).toBe("I'm a teapot")
-	})
+			expect(response.status).toBe(418)
+			expect(await response.text()).toBe("I'm a teapot")
+		}
+	)
 
 	it('handle error in order', async () => {
 		let order = <string[]>[]