Next.JS – little decorator (Higher Order Function) to handle Prisma errors

I am quite surprised I couldn’t find any generic way to handle exception from prisma client, so I wrote my own decorator.
I am just starting learning Next.js, so my approach could be very naive though, let me know if you see any improvements here.

It…


This content originally appeared on DEV Community and was authored by Mariusz

I am quite surprised I couldn't find any generic way to handle exception from prisma client, so I wrote my own decorator.
I am just starting learning Next.js, so my approach could be very naive though, let me know if you see any improvements here.

It hides the error details for any other environment than dev.

import { NextApiRequest, NextApiResponse } from "next";
import { isDevEnv } from "./common";
import { Prisma } from "@prisma/client";

export function withPrismaError(request: (req: NextApiRequest, res: NextApiResponse) => unknown) {
  return async function (req: NextApiRequest, res: NextApiResponse) {
    try {
      await request(req, res);
    } catch (e) {
      if (e instanceof Prisma.PrismaClientKnownRequestError) {
        const errorResponse = {
          error: isDevEnv ? `${e.code} ${e.message}` : "prisma_error",
        };

        return res.status(503).json(errorResponse);
      }

      return res.status(503).json({ error: true });
    }
  };
}

and the usage:

export default withPrismaError(
  async function handler(req: NextApiRequest, res: NextApiResponse) {
    const { email, password, url } = req.body;

    await registerUser(email, password, url);
    res.status(200).json({ success: true });
  }
);

PS. ...and "hello guys", apparently today I am transitioned from "lurker" to blogger!


This content originally appeared on DEV Community and was authored by Mariusz


Print Share Comment Cite Upload Translate Updates
APA

Mariusz | Sciencx (2022-06-29T20:39:02+00:00) Next.JS – little decorator (Higher Order Function) to handle Prisma errors. Retrieved from https://www.scien.cx/2022/06/29/next-js-little-decorator-higher-order-function-to-handle-prisma-errors/

MLA
" » Next.JS – little decorator (Higher Order Function) to handle Prisma errors." Mariusz | Sciencx - Wednesday June 29, 2022, https://www.scien.cx/2022/06/29/next-js-little-decorator-higher-order-function-to-handle-prisma-errors/
HARVARD
Mariusz | Sciencx Wednesday June 29, 2022 » Next.JS – little decorator (Higher Order Function) to handle Prisma errors., viewed ,<https://www.scien.cx/2022/06/29/next-js-little-decorator-higher-order-function-to-handle-prisma-errors/>
VANCOUVER
Mariusz | Sciencx - » Next.JS – little decorator (Higher Order Function) to handle Prisma errors. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/06/29/next-js-little-decorator-higher-order-function-to-handle-prisma-errors/
CHICAGO
" » Next.JS – little decorator (Higher Order Function) to handle Prisma errors." Mariusz | Sciencx - Accessed . https://www.scien.cx/2022/06/29/next-js-little-decorator-higher-order-function-to-handle-prisma-errors/
IEEE
" » Next.JS – little decorator (Higher Order Function) to handle Prisma errors." Mariusz | Sciencx [Online]. Available: https://www.scien.cx/2022/06/29/next-js-little-decorator-higher-order-function-to-handle-prisma-errors/. [Accessed: ]
rf:citation
» Next.JS – little decorator (Higher Order Function) to handle Prisma errors | Mariusz | Sciencx | https://www.scien.cx/2022/06/29/next-js-little-decorator-higher-order-function-to-handle-prisma-errors/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.