Same error reported by @google-cloud/error-reporting when using AppCustomError and global error handler

19 Views Asked by At

I am seeing the same error reported when I am switching between two errors on different lines when I use a custom error AppCustomError in my case and a express global error handler. How could I make "Test error 1" and "Test error 2" reported separatly?

Here is the GetTextsData middleware fonction where the errors are occuring:

import AppCustomError from "../../errors/AppCustomError.js";
export default async(req,res,next)=>{
    try{
        if(1!=1){
                    throw new AppCustomError({isPublic:"public",logSeverity:"ERROR",statusCode:500,errorCode:10010,dataToLog:req.dataToLog,
                    message:"GetTextsData - This is a test Error 1"});
            }

            if(1===1){
                    throw new AppCustomError({isPublic:"public",logSeverity:"ERROR",statusCode:500,errorCode:10010,dataToLog:req.dataToLog,
                    message:"GetTextsData - This is a test Error 2"});
            }
    res.status(200).send("Success);
    }catch(error){        
        return next(error);
    }      
}

This is the AppCustomError custom Error class:

class AppCustomError extends Error{ //appCustomError is a custom Error class

    constructor(errorObject){
        super();
        this.message=errorObject.message;
        this.errorCode=errorObject.errorCode; 
        this.statusCode=errorObject.statusCode;
        this.status=errorObject.statusCode >= 400 && errorObject.statusCode < 500 ? "fail" : "error";
        this.logSeverity=errorObject.logSeverity;
        this.dataToLog=errorObject.dataToLog;

        if(errorObject.isPublic==="public"){
            this.isPublic=true;
        }else{
            this.isPublic=false;
        }  
        
    }    

}
export default AppCustomError;

This is the global error handler express middleware that will catch the errors:

import {ErrorReporting} from '@google-cloud/error-reporting';

const errorReporting = new ErrorReporting();

export default function(error, req, res, next) {
    errorReporting.report(error);
    res.status(error.statusCode).json({
            status:error.status,
            message:error.message
        });
}

Usually the global error handler does much more like also logging the error and sending information back to client but I simplified the code for this example.

Here is what I see in GCP Error reporting: Error reporting screenshot

How could each of those two errors could have their own Error reporting Error?

Thank you and have a nice day!

0

There are 0 best solutions below