How to pass exception message to user rather than "internal error"?

Hi!
I want to pass exception message which I see in debug mode to user in production mode.

Background:
I got ERP application where we continously add more stuff (functionality) and sometimes users need delete something in no standard work flow, then they get “internal error” because of foreign key constraint, but they don’t see the reason. I want to show them this reason, so I don’t have to check logs or turn on debug. I know that I can define some foreign keys as “on delete cascade” but in some variants I want them to clean up stuff manually and with awareness.

Is there only option to close $this->MyModel->delete() with try{} catch() in each affected controller? Or can I modify something to pass all “constraint errors” to user in production mode?

Version 3.1

AFAIK, With the default ExceptionRenderer and Error templates, if you pass an Exceptions that extends the ones included with cakephp the message will be shown.

Ex:

try {
   $user = $this->Users->get($id);
} catch (RecordNotFoundException $ex) {
   throw new NotFoundException('User not found', null, $ex);
}

// in other code
throw new BadRequestException('Missing data');
1 Like

@raul338 thanks for response, i had other things in progress, now I check your solution and it doesn’t work for \PDOException. In debug I see this hand made message but in production I got internal error.
So probably I must run through this Error & Exception Handling - 3.10 “change render” but my fast tries with copy paste does nothing, same error look.
Also I don’t know what is PDO Exception name in above class and if I “catch” only PDO with this new class what happens with not defined exceptions?
For now I added try/catch and echo $ex->getMessage();die;

You shouldn’t do that. It can expose info about your database structure, bad actors will use that against your website.
You should check the logs for the info about the exception (and the stacktrace)

Yes because when debug is enabled, shows all error with cake exception renderer. In production does not and only shows generic “An internal error ocurred”

See my example with try/catch, you can extend it for other exceptions

try {
   $user = $this->Users->get($id);
} catch (RecordNotFoundException $ex) {
   throw new NotFoundException('User not found', null, $ex);
} catch (\Exception $ex) {
   throw new InternalErrorException('Internal error, please try again later', null, $ex);
}

and then check the error_log (logs/error.log) for more info.

If its a PDOException, maybe you’re trying to select an invalid column? invalid sql? you can check the SQL log in debugkit (or enable Query Logging)

but I want to show them why there is an error :slight_smile: also it is system for closed group of people working in same firm. The point is so I do not need to check logs and to they be able to read message and delete manually associated things.

I also notice that PDO Exception maybe rendered as error500 which wasn’t obvious for me, so I will make some research what variables are passed there in some “free” time.