From f90ac399c135e4afc05a3ded64973f5c263ef295 Mon Sep 17 00:00:00 2001 From: Mike Laumann Bellika <5175031+MikeBellika@users.noreply.github.com> Date: Thu, 12 Jan 2023 10:28:13 +0100 Subject: [PATCH 1/6] docs: add description of returning status codes Add description of how to use `createError` and `event.node.res.statusCode` from server directory --- .../2.guide/2.directory-structure/1.server.md | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/docs/content/1.docs/2.guide/2.directory-structure/1.server.md b/docs/content/1.docs/2.guide/2.directory-structure/1.server.md index 4f2508640b4..6ed50bce1b6 100644 --- a/docs/content/1.docs/2.guide/2.directory-structure/1.server.md +++ b/docs/content/1.docs/2.guide/2.directory-structure/1.server.md @@ -163,6 +163,34 @@ export default defineEventHandler((event) => { }) ``` +### Error handling +If no errors are thrown a status code of `200 OK` will be returned. Any uncaught errors will return a `500 Internal Server Error` HTTP Error. + +To return other error codes, throw an exception with `createError` +```ts [server/api/validation/[id].ts] +export default defineEventHandler((event) => { + const id = parseInt(event.context.params.id) as number + if (!Number.isInteger(id)) { + throw createError({ + statusCode: 400, + statusMessage: 'ID should an integer', + }) + } + return 'All good' +}) +``` + +### Returning other status codes +To return other status codes, modify `event.node.res.statusCode`. + +For example to return `204 No Content` +```ts [server/api/validation/[id].ts] +export default defineEventHandler((event) => { + event.node.res.statusCode = 204 + return {} +}) +``` + ### Accessing Runtime Config ```ts [server/api/foo.ts] From 2c23d8e9634962475ca6a2d3b0cf8a358a2c7381 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Thu, 19 Jan 2023 12:01:57 +0000 Subject: [PATCH 2/6] style: lint --- docs/content/1.docs/2.guide/2.directory-structure/1.server.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/content/1.docs/2.guide/2.directory-structure/1.server.md b/docs/content/1.docs/2.guide/2.directory-structure/1.server.md index 6ed50bce1b6..cbd8c311c06 100644 --- a/docs/content/1.docs/2.guide/2.directory-structure/1.server.md +++ b/docs/content/1.docs/2.guide/2.directory-structure/1.server.md @@ -164,9 +164,11 @@ export default defineEventHandler((event) => { ``` ### Error handling + If no errors are thrown a status code of `200 OK` will be returned. Any uncaught errors will return a `500 Internal Server Error` HTTP Error. To return other error codes, throw an exception with `createError` + ```ts [server/api/validation/[id].ts] export default defineEventHandler((event) => { const id = parseInt(event.context.params.id) as number @@ -181,9 +183,11 @@ export default defineEventHandler((event) => { ``` ### Returning other status codes + To return other status codes, modify `event.node.res.statusCode`. For example to return `204 No Content` + ```ts [server/api/validation/[id].ts] export default defineEventHandler((event) => { event.node.res.statusCode = 204 From a63769131ef944efbea0978f3cc9441d66b6df71 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Thu, 19 Jan 2023 12:06:53 +0000 Subject: [PATCH 3/6] docs: use 202 as example --- .../content/1.docs/2.guide/2.directory-structure/1.server.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/docs/content/1.docs/2.guide/2.directory-structure/1.server.md b/docs/content/1.docs/2.guide/2.directory-structure/1.server.md index cbd8c311c06..c1432f9d613 100644 --- a/docs/content/1.docs/2.guide/2.directory-structure/1.server.md +++ b/docs/content/1.docs/2.guide/2.directory-structure/1.server.md @@ -186,12 +186,11 @@ export default defineEventHandler((event) => { To return other status codes, modify `event.node.res.statusCode`. -For example to return `204 No Content` +For example to return `202 Accepted` ```ts [server/api/validation/[id].ts] export default defineEventHandler((event) => { - event.node.res.statusCode = 204 - return {} + setResponseStatus(event, 202) }) ``` From 7e175e388f158bfb95c589c4a1cfdf063282774b Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Thu, 19 Jan 2023 12:07:13 +0000 Subject: [PATCH 4/6] chore: use setResponseStatus --- docs/content/1.docs/2.guide/2.directory-structure/1.server.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/content/1.docs/2.guide/2.directory-structure/1.server.md b/docs/content/1.docs/2.guide/2.directory-structure/1.server.md index c1432f9d613..f317e949d1c 100644 --- a/docs/content/1.docs/2.guide/2.directory-structure/1.server.md +++ b/docs/content/1.docs/2.guide/2.directory-structure/1.server.md @@ -184,7 +184,7 @@ export default defineEventHandler((event) => { ### Returning other status codes -To return other status codes, modify `event.node.res.statusCode`. +To return other status codes, you can use the `setResponseStatus` utility. For example to return `202 Accepted` From 432b5de8b21182804ba265acbaccd8d404b85df6 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Thu, 19 Jan 2023 12:07:53 +0000 Subject: [PATCH 5/6] chore: minor typos --- docs/content/1.docs/2.guide/2.directory-structure/1.server.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/content/1.docs/2.guide/2.directory-structure/1.server.md b/docs/content/1.docs/2.guide/2.directory-structure/1.server.md index f317e949d1c..5a8462ba10b 100644 --- a/docs/content/1.docs/2.guide/2.directory-structure/1.server.md +++ b/docs/content/1.docs/2.guide/2.directory-structure/1.server.md @@ -165,7 +165,7 @@ export default defineEventHandler((event) => { ### Error handling -If no errors are thrown a status code of `200 OK` will be returned. Any uncaught errors will return a `500 Internal Server Error` HTTP Error. +If no errors are thrown, a status code of `200 OK` will be returned. Any uncaught errors will return a `500 Internal Server Error` HTTP Error. To return other error codes, throw an exception with `createError` @@ -175,7 +175,7 @@ export default defineEventHandler((event) => { if (!Number.isInteger(id)) { throw createError({ statusCode: 400, - statusMessage: 'ID should an integer', + statusMessage: 'ID should be an integer', }) } return 'All good' From 941411642ec72e6e47eb62ef7c5b212c50eccc59 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Thu, 19 Jan 2023 12:08:23 +0000 Subject: [PATCH 6/6] chore: minor typo --- docs/content/1.docs/2.guide/2.directory-structure/1.server.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/content/1.docs/2.guide/2.directory-structure/1.server.md b/docs/content/1.docs/2.guide/2.directory-structure/1.server.md index 5a8462ba10b..99d21a6b988 100644 --- a/docs/content/1.docs/2.guide/2.directory-structure/1.server.md +++ b/docs/content/1.docs/2.guide/2.directory-structure/1.server.md @@ -186,7 +186,7 @@ export default defineEventHandler((event) => { To return other status codes, you can use the `setResponseStatus` utility. -For example to return `202 Accepted` +For example, to return `202 Accepted` ```ts [server/api/validation/[id].ts] export default defineEventHandler((event) => {