Skip to content

Commit

Permalink
Add further tests for error handling, improving overall coverage to 9…
Browse files Browse the repository at this point in the history
…4.2%
  • Loading branch information
briward committed Nov 12, 2024
1 parent c431cc6 commit 07ec308
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 21 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,23 +129,21 @@ Errors thrown in middleware are picked up and added to the `Context` object, all
```ts
import { type Context, NotFound } from "jsr:@raptor/framework";

// Simulate an application error.
app.add((context: Context) => {
throw new NotFound();
});

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

if (!error) {
return;
};

if (error.status === 404) {
return '<h1>No page could be found</h1>';
if (error?.status === 404) {
return '<h1>No page could be found</h1>'
}

response.status = 500;
return '<h1>There was a server error</h1>';
return '<h1>There was a server error</h1>'
});
```

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

You can create your own errors by implementing the `Error` interface.

# Deployment

## Deno Deploy
Expand Down
13 changes: 0 additions & 13 deletions src/http/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,4 @@ export default class HttpResponse extends Response {
override clone(): HttpResponse {
return this;
}

/**
* Check if the response has body content.
*
* @returns Whether the response has body content.
*/
public hasBody(): boolean {
const length = this.headers.get("content-length");

if (!length) return false;

return parseInt(length) > 0;
}
}
69 changes: 68 additions & 1 deletion tests/kernel.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { assertEquals } from "jsr:@std/assert";

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

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

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

assertEquals(await response.text(), "Hello from the second middleware");
});

Deno.test("test middleware catches 404 error", async () => {
const app = new Kernel();

app.add((_ctx: Context) => {
throw new NotFound();
});

app.add((ctx: Context) => {
const { error } = ctx;

if (error?.status === 404) {
return 'Page not found';
}
});

const response = await app.respond(new Request(APP_URL));

assertEquals(await response.text(), "Page not found");
});

Deno.test("test middleware catches server error", async () => {
const app = new Kernel();

app.add((_ctx: Context) => {
throw new ServerError();
});

app.add((ctx: Context) => {
const { error } = ctx;

if (error?.status === 500) {
return 'Internal server error';
}
});

const response = await app.respond(new Request(APP_URL));

assertEquals(await response.text(), "Internal server error");
});

Deno.test("test middleware catches bad request error", async () => {
const app = new Kernel();

app.add((_ctx: Context) => {
throw new BadRequest([
'There was an error in validation of field #1',
'There was an error in validation of field #2'
]);
});

app.add((ctx: Context) => {
const { error } = ctx;

if (error?.status === 400) {
return 'Bad request';
}
});

const response = await app.respond(new Request(APP_URL));

assertEquals(await response.text(), "Bad request");
});

0 comments on commit 07ec308

Please sign in to comment.