Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Add error response samples to openapi

Kiran Kd95db495e2ca
diff --git a/apps/web/lib/errors.ts b/apps/web/lib/errors.ts
index 054cd19b22..889fd4506e 100644
--- a/apps/web/lib/errors.ts
+++ b/apps/web/lib/errors.ts
@@ -21,16 +21,16 @@ const ErrorCode = z.enum([
const errorCodeToHttpStatus: Record<z.infer<typeof ErrorCode>, number> = {
bad_request: 400,
- not_found: 404,
- internal_server_error: 500,
unauthorized: 401,
forbidden: 403,
- rate_limit_exceeded: 429,
- invite_expired: 410,
- invite_pending: 409,
exceeded_limit: 403,
+ not_found: 404,
conflict: 409,
+ invite_pending: 409,
+ invite_expired: 410,
unprocessible_entity: 422,
+ rate_limit_exceeded: 429,
+ internal_server_error: 500,
};
const prismaErrorMapping: Record<
@@ -156,3 +156,26 @@ export function handleAndReturnErrorResponse(
console.error(error);
return NextResponse.json<ErrorResponse>({ error }, { headers, status });
}
+
+export const errorSchemaFactory = (code: z.infer<typeof ErrorCode>) => {
+ return z.object({
+ error: z.object({
+ code: z.literal(code).openapi({
+ description: "A short code indicating the error code returned.",
+ example: code,
+ }),
+ message: z.string().openapi({
+ description: "A human readable explanation of what went wrong.",
+ example: "The requested resource was not found.",
+ }),
+ doc_url: z
+ .string()
+ .optional()
+ .openapi({
+ description:
+ "A link to our documentation with more details about this error code",
+ example: `${docErrorUrl}#${code}`,
+ }),
+ }),
+ });
+};
diff --git a/apps/web/lib/openapi/links/create-bulk-link.ts b/apps/web/lib/openapi/links/create-bulk-link.ts
index b3d2006915..cbc8a2ac1b 100644
--- a/apps/web/lib/openapi/links/create-bulk-link.ts
+++ b/apps/web/lib/openapi/links/create-bulk-link.ts
@@ -6,6 +6,7 @@ import {
GetLinkInfoQuerySchema,
} from "@/lib/zod/schemas/links";
import z from "@/lib/zod";
+import { openApiErrorResponses } from "@/lib/openapi/responses";
export const createBulkLink: ZodOpenApiOperationObject = {
operationId: "bulkCreateLinks",
@@ -30,6 +31,7 @@ export const createBulkLink: ZodOpenApiOperationObject = {
},
},
},
+ ...openApiErrorResponses,
},
tags: ["Links"],
security: [{ bearerToken: [] }],
diff --git a/apps/web/lib/openapi/links/create-link.ts b/apps/web/lib/openapi/links/create-link.ts
index e7b45d875c..38782b6697 100644
--- a/apps/web/lib/openapi/links/create-link.ts
+++ b/apps/web/lib/openapi/links/create-link.ts
@@ -5,6 +5,7 @@ import {
LinkSchema,
GetLinkInfoQuerySchema,
} from "@/lib/zod/schemas/links";
+import { openApiErrorResponses } from "@/lib/openapi/responses";
export const createLink: ZodOpenApiOperationObject = {
operationId: "createLink",
@@ -29,6 +30,7 @@ export const createLink: ZodOpenApiOperationObject = {
},
},
},
+ ...openApiErrorResponses,
},
tags: ["Links"],
security: [{ bearerToken: [] }],
diff --git a/apps/web/lib/openapi/links/delete-link.ts b/apps/web/lib/openapi/links/delete-link.ts
index 190ae3784b..0037f0b401 100644
--- a/apps/web/lib/openapi/links/delete-link.ts
+++ b/apps/web/lib/openapi/links/delete-link.ts
@@ -2,6 +2,7 @@ import { ZodOpenApiOperationObject } from "zod-openapi";
import z from "@/lib/zod";
import { GetLinksQuerySchema, LinkSchema } from "@/lib/zod/schemas/links";
+import { openApiErrorResponses } from "@/lib/openapi/responses";
export const deleteLink: ZodOpenApiOperationObject = {
operationId: "deleteLink",
@@ -25,6 +26,7 @@ export const deleteLink: ZodOpenApiOperationObject = {
},
},
},
+ ...openApiErrorResponses,
},
tags: ["Links"],
security: [{ bearerToken: [] }],
diff --git a/apps/web/lib/openapi/links/edit-link.ts b/apps/web/lib/openapi/links/edit-link.ts
index 1244027011..22dc0df6c1 100644
--- a/apps/web/lib/openapi/links/edit-link.ts
+++ b/apps/web/lib/openapi/links/edit-link.ts
@@ -6,6 +6,7 @@ import {
GetLinkInfoQuerySchema,
} from "@/lib/zod/schemas/links";
import z from "@/lib/zod";
+import { openApiErrorResponses } from "@/lib/openapi/responses";
export const editLink: ZodOpenApiOperationObject = {
operationId: "editLink",
@@ -36,6 +37,7 @@ export const editLink: ZodOpenApiOperationObject = {
},
},
},
+ ...openApiErrorResponses,
},
tags: ["Links"],
security: [{ bearerToken: [] }],
diff --git a/apps/web/lib/openapi/links/get-link-info.ts b/apps/web/lib/openapi/links/get-link-info.ts
index 5e65425604..ef81668428 100644
--- a/apps/web/lib/openapi/links/get-link-info.ts
+++ b/apps/web/lib/openapi/links/get-link-info.ts
@@ -1,6 +1,7 @@
import { ZodOpenApiOperationObject } from "zod-openapi";
import { GetLinkInfoQuerySchema, LinkSchema } from "@/lib/zod/schemas/links";
+import { openApiErrorResponses } from "@/lib/openapi/responses";
export const getLinkInfo: ZodOpenApiOperationObject = {
operationId: "getLinkInfo",
@@ -18,6 +19,7 @@ export const getLinkInfo: ZodOpenApiOperationObject = {
},
},
},
+ ...openApiErrorResponses,
},
tags: ["Links"],
security: [{ bearerToken: [] }],
diff --git a/apps/web/lib/openapi/links/get-links.ts b/apps/web/lib/openapi/links/get-links.ts
index 278faf9a7a..220692c710 100644
--- a/apps/web/lib/openapi/links/get-links.ts
+++ b/apps/web/lib/openapi/links/get-links.ts
@@ -2,6 +2,7 @@ import { ZodOpenApiOperationObject } from "zod-openapi";
import z from "@/lib/zod";
import { GetLinksQuerySchema, LinkSchema } from "@/lib/zod/schemas/links";
+import { openApiErrorResponses } from "@/lib/openapi/responses";
export const getLinks: ZodOpenApiOperationObject = {
operationId: "getLinks",
@@ -20,6 +21,7 @@ export const getLinks: ZodOpenApiOperationObject = {
},
},
},
+ ...openApiErrorResponses,
},
tags: ["Links"],
security: [{ bearerToken: [] }],
diff --git a/apps/web/lib/openapi/links/get-qr.ts b/apps/web/lib/openapi/links/get-qr.ts
index 0671dae899..a19d9e73fa 100644
--- a/apps/web/lib/openapi/links/get-qr.ts
+++ b/apps/web/lib/openapi/links/get-qr.ts
@@ -2,6 +2,7 @@ import { ZodOpenApiOperationObject } from "zod-openapi";
import z from "@/lib/zod";
import { GetQRCodeQuerySchema } from "@/lib/zod/schemas/qr";
+import { openApiErrorResponses } from "@/lib/openapi/responses";
export const getQRCode: ZodOpenApiOperationObject = {
operationId: "getQRCode",
@@ -19,6 +20,7 @@ export const getQRCode: ZodOpenApiOperationObject = {
},
},
},
+ ...openApiErrorResponses,
},
tags: ["Links"],
security: [{ bearerToken: [] }],
diff --git a/apps/web/lib/openapi/projects/get-project-info.ts b/apps/web/lib/openapi/projects/get-project-info.ts
index e151540019..65a6133f76 100644
--- a/apps/web/lib/openapi/projects/get-project-info.ts
+++ b/apps/web/lib/openapi/projects/get-project-info.ts
@@ -2,6 +2,7 @@ import { ZodOpenApiOperationObject } from "zod-openapi";
import z from "@/lib/zod";
import { ProjectSchema } from "@/lib/zod/schemas/projects";
+import { openApiErrorResponses } from "@/lib/openapi/responses";
export const getProjectInfo: ZodOpenApiOperationObject = {
operationId: "getProject",
@@ -25,6 +26,7 @@ export const getProjectInfo: ZodOpenApiOperationObject = {
},
},
},
+ ...openApiErrorResponses,
},
tags: ["Projects"],
security: [{ bearerToken: [] }],
diff --git a/apps/web/lib/openapi/projects/get-projects.ts b/apps/web/lib/openapi/projects/get-projects.ts
index 6a6a1af289..9ff008dda2 100644
--- a/apps/web/lib/openapi/projects/get-projects.ts
+++ b/apps/web/lib/openapi/projects/get-projects.ts
@@ -2,6 +2,7 @@ import { ZodOpenApiOperationObject } from "zod-openapi";
import z from "@/lib/zod";
import { ProjectSchema } from "@/lib/zod/schemas/projects";
+import { openApiErrorResponses } from "@/lib/openapi/responses";
export const getProjects: ZodOpenApiOperationObject = {
operationId: "getProjects",
@@ -16,6 +17,7 @@ export const getProjects: ZodOpenApiOperationObject = {
},
},
},
+ ...openApiErrorResponses,
},
tags: ["Projects"],
security: [{ bearerToken: [] }],
diff --git a/apps/web/lib/openapi/responses.ts b/apps/web/lib/openapi/responses.ts
new file mode 100644
index 0000000000..3bd3e05f53
--- /dev/null
+++ b/apps/web/lib/openapi/responses.ts
@@ -0,0 +1,90 @@
+import { errorSchemaFactory } from "../errors";
+
+export const openApiErrorResponses = {
+ 400: {
+ description:
+ "The server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).",
+ content: {
+ "application/json": {
+ schema: errorSchemaFactory("bad_request"),
+ },
+ },
+ },
+
+ 401: {
+ description: `Although the HTTP standard specifies "unauthorized", semantically this response means "unauthenticated". That is, the client must authenticate itself to get the requested response.`,
+ content: {
+ "application/json": {
+ schema: errorSchemaFactory("unauthorized"),
+ },
+ },
+ },
+
+ 403: {
+ description:
+ "The client does not have access rights to the content; that is, it is unauthorized, so the server is refusing to give the requested resource. Unlike 401 Unauthorized, the client's identity is known to the server.",
+ content: {
+ "application/json": {
+ schema: errorSchemaFactory("forbidden"),
+ },
+ },
+ },
+
+ 404: {
+ description: "The server cannot find the requested resource.",
+ content: {
+ "application/json": {
+ schema: errorSchemaFactory("not_found"),
+ },
+ },
+ },
+
+ 409: {
+ description:
+ "This response is sent when a request conflicts with the current state of the server.",
+ content: {
+ "application/json": {
+ schema: errorSchemaFactory("conflict"),
+ },
+ },
+ },
+
+ 410: {
+ description:
+ "This response is sent when the requested content has been permanently deleted from server, with no forwarding address.",
+ content: {
+ "application/json": {
+ schema: errorSchemaFactory("invite_expired"),
+ },
+ },
+ },
+
+ 422: {
+ description:
+ "The request was well-formed but was unable to be followed due to semantic errors.",
+ content: {
+ "application/json": {
+ schema: errorSchemaFactory("unprocessible_entity"),
+ },
+ },
+ },
+
+ 429: {
+ description: `The user has sent too many requests in a given amount of time ("rate limiting")`,
+ content: {
+ "application/json": {
+ schema: errorSchemaFactory("rate_limit_exceeded"),
+ },
+ },
+ },
+
+ 500: {
+ description:
+ "The server has encountered a situation it does not know how to handle.",
+ content: {
+ "application/json": {
+ schema: errorSchemaFactory("internal_server_error"),
+ },
+ },
+ },
+};
diff --git a/apps/web/lib/openapi/tags/create-tag.ts b/apps/web/lib/openapi/tags/create-tag.ts
index fece5e4fe9..0869308d36 100644
--- a/apps/web/lib/openapi/tags/create-tag.ts
+++ b/apps/web/lib/openapi/tags/create-tag.ts
@@ -2,6 +2,7 @@ import { ZodOpenApiOperationObject } from "zod-openapi";
import z from "@/lib/zod";
import { CreateTagBodySchema, TagSchema } from "@/lib/zod/schemas/tags";
+import { openApiErrorResponses } from "@/lib/openapi/responses";
export const createTag: ZodOpenApiOperationObject = {
operationId: "createTag",
@@ -32,6 +33,7 @@ export const createTag: ZodOpenApiOperationObject = {
},
},
},
+ ...openApiErrorResponses,
},
tags: ["Tags"],
security: [{ bearerToken: [] }],
diff --git a/apps/web/lib/openapi/tags/get-tags.ts b/apps/web/lib/openapi/tags/get-tags.ts
index d120294fcd..9bf9e84613 100644
--- a/apps/web/lib/openapi/tags/get-tags.ts
+++ b/apps/web/lib/openapi/tags/get-tags.ts
@@ -2,6 +2,7 @@ import { ZodOpenApiOperationObject } from "zod-openapi";
import z from "@/lib/zod";
import { TagSchema } from "@/lib/zod/schemas/tags";
+import { openApiErrorResponses } from "@/lib/openapi/responses";
export const getTags: ZodOpenApiOperationObject = {
operationId: "getTags",
@@ -25,6 +26,7 @@ export const getTags: ZodOpenApiOperationObject = {
},
},
},
+ ...openApiErrorResponses,
},
tags: ["Tags"],
security: [{ bearerToken: [] }],