Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

376
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda