callbackAfterUpload with custom error message

I am trying to use callbackAfterUpload with custom error messages. I am reading the excel and validating if there is any value of a cell before inserting to database. Hence using validation.
For e.g. to check a valid department, I am using this. Now problem with this the error it throws gives error code 500 and no clear description of error on screen .

if ( ! array_key_exists($depttShName, $departsArr)) {
                    return $errorMessage->setMessage("$depttShName is not a valid department. Please check row = $row, and column = 1 \n");
                  }

Hey @johnny - Please throw some lights here.

Hello @kapilchugh ,

hmmm that’s weird! Maybe there is a fatal error (e.g. $row doesn’t exist) and it is throwing a 500 error (e.g. white page?). Can you try the same without showing the $row? For example:

return $errorMessage->setMessage("$depttShName is not a valid department. \n");

or even:

return $errorMessage->setMessage("The departmenet is not a valid department. \n");

If this is working for you then maybe and the $row exist then you can use it like this:

return $errorMessage->setMessage("$depttShName is not a valid department. 
Please check row = " . print_r($row, true) .", and column = 1 \n");

To be honest, I would challenge the use of callbackAfterUpload though that this is not the correct approach. Although I don’t know your full example, how from the upload field you can have such a validation :thinking:? Maybe it is better to use callbackBeforeInsert or callbackBeforeUpdate instead?