Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

372
Views
NextJS custom _error page can't catch error details for server side errors in the production build

I am trying to catch NextJS serverside errors and flush the error messages to the sentry.

It is flushing messages without error details.

Here is the getServerSideProps error that I use try-catch block to catch an error in a nextjs page.

export const getServerSideProps: GetServerSideProps = async ({ locale }) => {
    try {
        throw new Error(`Server side error has been occured`)
        return {
            props: {
// SSR props
            }
        }
    } catch (error) {
        throw error
    }
    return { props: {} }
}

Here is the _error.tsx page implementation:

    import * as Sentry from "@sentry/nextjs"
    import NextErrorComponent, {ErrorProps as NextErrorProps} from "next/error"
    import {NextPageContext} from "next"
    // import envs from 'helpers/envs';
    
    export type ErrorPageProps = {
        err: Error
        statusCode: number
    }
    
    export type ErrorProps = {
        isReadyToRender: boolean
    } & NextErrorProps
    
    const ErrorPage = (props: ErrorPageProps): JSX.Element => {
        const {statusCode, err} = props
        // eslint-disable-next-line no-debugger

        // the problem is in here. err comes as undefined.
        if (err) {
            Sentry.captureException(err)
        }
    
        return (
            <p>
                {statusCode ? `An error ${statusCode} occurred on server` : "An error occurred on client"}
            </p>
        )
    }
    
    ErrorPage.getInitialProps = async (props: NextPageContext): Promise<ErrorProps> => {
        const {res, err, asPath} = props
        console.log(err)
    
        const errorInitialProps: ErrorProps = (await NextErrorComponent.getInitialProps({
            res,
            err
        } as NextPageContext)) as ErrorProps
    
        errorInitialProps.isReadyToRender = true
    
        if (res?.statusCode === 404) {
            return {statusCode: 404, isReadyToRender: true}
        }
    
        if (err) {
            Sentry.captureException(err)
            await Sentry.flush(2000)
            return errorInitialProps
        }
    
        Sentry.captureException(new Error(`_error.js getInitialProps missing data at path: ${asPath}`))
        await Sentry.flush(2000)
    
        return errorInitialProps
    }
    
    export default ErrorPage

How can I get error details from SSR and send to the sentry?

Any help will be appreciated.

Thanks

about 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!