|
1 |
| -import { expect, test, expectTypeOf, assertType } from "vitest"; |
2 |
| -import { createObservedClient } from "../helpers.js"; |
| 1 | +import { assertType, expect, expectTypeOf, test } from "vitest"; |
3 | 2 | import type { Middleware, MiddlewareCallbackParams } from "../../src/index.js";
|
| 3 | +import { createObservedClient } from "../helpers.js"; |
4 | 4 | import type { paths } from "./schemas/middleware.js";
|
5 | 5 |
|
6 | 6 | test("receives a UUID per-request", async () => {
|
@@ -96,6 +96,62 @@ test("can modify response", async () => {
|
96 | 96 | expect(response.headers.get("middleware")).toBe("value");
|
97 | 97 | });
|
98 | 98 |
|
| 99 | +test("returns original errors if nothing is returned", async () => { |
| 100 | + const actualError = new Error(); |
| 101 | + const client = createObservedClient<paths>({}, async (req) => { |
| 102 | + throw actualError; |
| 103 | + }); |
| 104 | + client.use({ |
| 105 | + onError({ error }) { |
| 106 | + expect(error).toBe(actualError); |
| 107 | + return; |
| 108 | + }, |
| 109 | + }); |
| 110 | + |
| 111 | + try { |
| 112 | + await client.GET("/posts/{id}", { params: { path: { id: 123 } } }); |
| 113 | + } catch (thrownError) { |
| 114 | + expect(thrownError).toBe(actualError); |
| 115 | + } |
| 116 | +}); |
| 117 | + |
| 118 | +test("can modify errors", async () => { |
| 119 | + const actualError = new Error(); |
| 120 | + const modifiedError = new Error(); |
| 121 | + const client = createObservedClient<paths>({}, async (req) => { |
| 122 | + throw actualError; |
| 123 | + }); |
| 124 | + client.use({ |
| 125 | + onError() { |
| 126 | + return modifiedError; |
| 127 | + }, |
| 128 | + }); |
| 129 | + |
| 130 | + try { |
| 131 | + await client.GET("/posts/{id}", { params: { path: { id: 123 } } }); |
| 132 | + } catch (thrownError) { |
| 133 | + expect(thrownError).toBe(modifiedError); |
| 134 | + } |
| 135 | +}); |
| 136 | + |
| 137 | +test("can catch errors and return a response instead", async () => { |
| 138 | + const actualError = new Error(); |
| 139 | + const customResponse = Response.json({}); |
| 140 | + const client = createObservedClient<paths>({}, async (req) => { |
| 141 | + throw actualError; |
| 142 | + }); |
| 143 | + client.use({ |
| 144 | + onError({ error }) { |
| 145 | + expect(error).toBe(actualError); |
| 146 | + return customResponse; |
| 147 | + }, |
| 148 | + }); |
| 149 | + |
| 150 | + const { response } = await client.GET("/posts/{id}", { params: { path: { id: 123 } } }); |
| 151 | + |
| 152 | + expect(response).toBe(customResponse); |
| 153 | +}); |
| 154 | + |
99 | 155 | test("executes in expected order", async () => {
|
100 | 156 | let actualRequest = new Request("https://nottherealurl.fake");
|
101 | 157 | const client = createObservedClient<paths>({}, async (req) => {
|
@@ -153,6 +209,30 @@ test("executes in expected order", async () => {
|
153 | 209 | expect(response.headers.get("step")).toBe("A");
|
154 | 210 | });
|
155 | 211 |
|
| 212 | +test("executes error handlers in expected order", async () => { |
| 213 | + const actualError = new Error(); |
| 214 | + const modifiedError = new Error(); |
| 215 | + const customResponse = Response.json({}); |
| 216 | + const client = createObservedClient<paths>({}, async (req) => { |
| 217 | + throw actualError; |
| 218 | + }); |
| 219 | + client.use({ |
| 220 | + onError({ error }) { |
| 221 | + expect(error).toBe(modifiedError); |
| 222 | + return customResponse; |
| 223 | + }, |
| 224 | + }); |
| 225 | + client.use({ |
| 226 | + onError() { |
| 227 | + return modifiedError; |
| 228 | + }, |
| 229 | + }); |
| 230 | + |
| 231 | + const { response } = await client.GET("/posts/{id}", { params: { path: { id: 123 } } }); |
| 232 | + |
| 233 | + expect(response).toBe(customResponse); |
| 234 | +}); |
| 235 | + |
156 | 236 | test("receives correct options", async () => {
|
157 | 237 | let requestBaseUrl = "";
|
158 | 238 |
|
|
0 commit comments