-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: π error handling improvements & docs
- Loading branch information
Evert De Spiegeleer
committed
Jan 13, 2024
1 parent
31fe678
commit c416cfc
Showing
7 changed files
with
196 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { z } from 'zod' | ||
import { controller, get } from '@zhttp/core' | ||
import { NotFoundError } from '@zhttp/errors' | ||
|
||
// Let's presume we're talking to some sort of database | ||
const db: any = undefined | ||
|
||
export const vegetablesController = controller('vegetables') | ||
|
||
vegetablesController.endpoint( | ||
get('/vegetables/:vegetableId', 'getVegetableDetails') | ||
.input(z.object({ | ||
params: z.object({ | ||
vegetableId: z.string().uuid() | ||
}) | ||
})) | ||
.response(z.object({ | ||
message: z.string() | ||
})) | ||
.handler(async ({ params: { vegetableId } }) => { | ||
const vegetableDetails = await db.getVegetableById(vegetableId) | ||
if (vegetableDetails == null) { | ||
// β¨β¨β¨β¨β¨β¨β¨β¨β¨ | ||
throw new NotFoundError(`Vegetable with id ${vegetableId} does not exist`) | ||
// β¬ This will result in a 404 response | ||
// β¨β¨β¨β¨β¨β¨β¨β¨β¨ | ||
} | ||
return vegetableDetails | ||
}) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { z } from 'zod' | ||
import { controller, get } from '@zhttp/core' | ||
|
||
export const validationExampleController = controller('validationExample') | ||
|
||
validationExampleController.endpoint( | ||
get('/hello', 'getGreeting') | ||
.input(z.object({ | ||
query: z.object({ | ||
// If a name shorter than 5 characcters is provided, then the server will responde with a ValidationError. | ||
name: z.string().min(5) | ||
}) | ||
})) | ||
.response(z.object({ | ||
message: z.string() | ||
})) | ||
.handler(async ({ query }) => { | ||
return { | ||
message: `Hello ${query.name ?? 'everyone'}!` | ||
} | ||
}) | ||
) | ||
|
||
validationExampleController.endpoint( | ||
get('/goodbye', 'getGoodbye') | ||
.input(z.object({ | ||
query: z.object({ | ||
name: z.string().optional() | ||
}) | ||
})) | ||
.response(z.object({ | ||
message: z.string() | ||
})) | ||
.handler(async ({ query }) => { | ||
return { | ||
thisKeyShouldntBeHere: 'noBueno' | ||
} as any | ||
// β¬ As zhttp is typesafe, you actually have to manually $x&! up the typing | ||
// to provoke an output validation error :) | ||
// This will result in an InternalServerError. | ||
}) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters