Trapping exceptions to show on the user progress bar

Hello, I have a BackgroundJob like this

void process(int id) {
     NotifyProcess(id, "Process started");
     DoProcess1();
     NotifyProcess(id, "20%");
     DoProcess2();
     NotifyProcess(id, "40%");
     DoProcess3();
     NotifyProcess(id, "60%");
     DoProcess4();
     NotifyProcess(id, "80%");
     DoProcess5();
     NotifyProcess(id, "Complete");
}

It is a very simple example of my notify process witch shows an incremental progress bar to the user, that works ok.

But, the problem is if maybe DoProcess4(), fails and throws an exception, in that case I would want to call:

NotifyProcess(id, "Error");

I easily could trap errors with try-catch an notify them, but the job is shown as Success on the Hangfire Dashboard, and that’s not what I want.

Any suggestion?

I think I have to do something with the logging interface, but how can I pass the notification id?

Thank you!!
BR

Well, I finished typing and I found the simple solution, shared:

void process(int id) {
   try {
     NotifyProcess(id, "Process started");
     DoProcess1();
     NotifyProcess(id, "20%");
     DoProcess2();
     NotifyProcess(id, "40%");
     DoProcess3();
     NotifyProcess(id, "60%");
     DoProcess4();
     NotifyProcess(id, "80%");
     DoProcess5();
     NotifyProcess(id, "Complete");
  } catch (Exception ex) {
      NotifyProcess(id, "process failed");
      throw new Exception("process failed", ex);
  }
}

The “Process failed” message in the wrapping exception doesn’t really add any information, so you could just use:

throw;

… to re-throw the original exception in the catch block.