Skip to content

Commit 07ec308

Browse files
committed
Add further tests for error handling, improving overall coverage to 94.2%
1 parent c431cc6 commit 07ec308

File tree

3 files changed

+75
-21
lines changed

3 files changed

+75
-21
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -129,23 +129,21 @@ Errors thrown in middleware are picked up and added to the `Context` object, all
129129
```ts
130130
import { type Context, NotFound } from "jsr:@raptor/framework";
131131

132+
// Simulate an application error.
132133
app.add((context: Context) => {
133134
throw new NotFound();
134135
});
135136

137+
// Catch our error and handle response.
136138
app.add((context: Context) => {
137139
const { error, response } = context;
138140

139-
if (!error) {
140-
return;
141-
};
142-
143-
if (error.status === 404) {
144-
return '<h1>No page could be found</h1>';
141+
if (error?.status === 404) {
142+
return '<h1>No page could be found</h1>'
145143
}
146144

147145
response.status = 500;
148-
return '<h1>There was a server error</h1>';
146+
return '<h1>There was a server error</h1>'
149147
});
150148
```
151149

@@ -156,6 +154,8 @@ The following errors are currently available to import and throw from within the
156154
* `ServerError`
157155
* `TypeError`
158156

157+
You can create your own errors by implementing the `Error` interface.
158+
159159
# Deployment
160160

161161
## Deno Deploy

src/http/response.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,4 @@ export default class HttpResponse extends Response {
2929
override clone(): HttpResponse {
3030
return this;
3131
}
32-
33-
/**
34-
* Check if the response has body content.
35-
*
36-
* @returns Whether the response has body content.
37-
*/
38-
public hasBody(): boolean {
39-
const length = this.headers.get("content-length");
40-
41-
if (!length) return false;
42-
43-
return parseInt(length) > 0;
44-
}
4532
}

tests/kernel.test.ts

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { assertEquals } from "jsr:@std/assert";
22

3-
import { type Context, Kernel } from "../mod.ts";
3+
import Kernel from "../src/kernel.ts";
4+
import NotFound from "../src/error/not-found.ts";
5+
import type Context from "../src/http/context.ts";
6+
import BadRequest from "../src/error/bad-request.ts";
7+
import ServerError from "../src/error/server-error.ts";
48

59
const APP_URL = "http://localhost:8000";
610

@@ -68,3 +72,66 @@ Deno.test("test middleware next callback functionality", async () => {
6872

6973
assertEquals(await response.text(), "Hello from the second middleware");
7074
});
75+
76+
Deno.test("test middleware catches 404 error", async () => {
77+
const app = new Kernel();
78+
79+
app.add((_ctx: Context) => {
80+
throw new NotFound();
81+
});
82+
83+
app.add((ctx: Context) => {
84+
const { error } = ctx;
85+
86+
if (error?.status === 404) {
87+
return 'Page not found';
88+
}
89+
});
90+
91+
const response = await app.respond(new Request(APP_URL));
92+
93+
assertEquals(await response.text(), "Page not found");
94+
});
95+
96+
Deno.test("test middleware catches server error", async () => {
97+
const app = new Kernel();
98+
99+
app.add((_ctx: Context) => {
100+
throw new ServerError();
101+
});
102+
103+
app.add((ctx: Context) => {
104+
const { error } = ctx;
105+
106+
if (error?.status === 500) {
107+
return 'Internal server error';
108+
}
109+
});
110+
111+
const response = await app.respond(new Request(APP_URL));
112+
113+
assertEquals(await response.text(), "Internal server error");
114+
});
115+
116+
Deno.test("test middleware catches bad request error", async () => {
117+
const app = new Kernel();
118+
119+
app.add((_ctx: Context) => {
120+
throw new BadRequest([
121+
'There was an error in validation of field #1',
122+
'There was an error in validation of field #2'
123+
]);
124+
});
125+
126+
app.add((ctx: Context) => {
127+
const { error } = ctx;
128+
129+
if (error?.status === 400) {
130+
return 'Bad request';
131+
}
132+
});
133+
134+
const response = await app.respond(new Request(APP_URL));
135+
136+
assertEquals(await response.text(), "Bad request");
137+
});

0 commit comments

Comments
 (0)