How to return a page with 403 status code in nextjs14

123 Views Asked by At

I am using Next14 (14.0.4) and developing a simple EditPost page. I would like to check if user is allowed to edit the post and show a proper error page.

const EditPost = async () => {
    const session = await getServerSession()

    if (!notAuthorised(session)) {
        throw new Error('Not allowed')
    }
    ....

I have a simple error.js page in my root folder:

const BlogError = ({ error }) => 
  <div>
    <h1>Error occured</h1>
    <p> { error.message } </p>
</div>)

export default BlogError

And unauthorised user is getting redirect to the page. The problem, is that the response has 500 status code, which is not desired. How to show the user error page with 403 status?

0

There are 0 best solutions below