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

**URL:** <https://discourse.cakephp.org/t/how-to-pass-exception-message-to-user-rather-than-internal-error/9656>\
**Category:** Need Help\
**Created:** [August 23, 2021, 9:26am UTC](https://discourse.cakephp.org/t/how-to-pass-exception-message-to-user-rather-than-internal-error/9656 "2021-08-23T09:26:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jarekgol](https://avatars.discourse-cdn.com/v4/letter/j/58956e/32.png) [@jarekgol](https://discourse.cakephp.org/u/jarekgol)\
**Post date:** [August 23, 2021, 9:26am UTC](https://discourse.cakephp.org/t/how-to-pass-exception-message-to-user-rather-than-internal-error/9656/1 "2021-08-23T09:26:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![raul338](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/raul338/32/69_2.png) [@raul338](https://discourse.cakephp.org/u/raul338)\
**Post date:** [August 23, 2021, 7:19pm UTC](https://discourse.cakephp.org/t/how-to-pass-exception-message-to-user-rather-than-internal-error/9656/2 "2021-08-23T19:19:04Z")

</div>

AFAIK, With the default ExceptionRenderer and Error templates, if you pass an Exceptions that extends the ones [included with cakephp](https://book.cakephp.org/4/en/development/errors.html#built-in-exceptions-for-cakephp) the message will be shown.

Ex:

```php
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');

```

---

<div class="post-metadata">

**Author:** ![jarekgol](https://avatars.discourse-cdn.com/v4/letter/j/58956e/32.png) [@jarekgol](https://discourse.cakephp.org/u/jarekgol)\
**Post date:** [August 30, 2021, 3:52pm UTC](https://discourse.cakephp.org/t/how-to-pass-exception-message-to-user-rather-than-internal-error/9656/3 "2021-08-30T15:52:04Z")

</div>

@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](https://book.cakephp.org/3/en/development/errors.html#change-the-exceptionrenderer) “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;`

---

<div class="post-metadata">

**Author:** ![raul338](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/raul338/32/69_2.png) [@raul338](https://discourse.cakephp.org/u/raul338)\
**Post date:** [August 30, 2021, 11:28pm UTC](https://discourse.cakephp.org/t/how-to-pass-exception-message-to-user-rather-than-internal-error/9656/4 "2021-08-30T23:28:31Z")

</div>

> [@jarekgol](#):
>
> 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)

> [@jarekgol](#):
>
> 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.

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”

> [@jarekgol](#):
>
> 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?

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

```auto
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](https://book.cakephp.org/4/en/orm/database-basics.html#query-logging))

---

<div class="post-metadata">

**Author:** ![jarekgol](https://avatars.discourse-cdn.com/v4/letter/j/58956e/32.png) [@jarekgol](https://discourse.cakephp.org/u/jarekgol)\
**Post date:** [September 3, 2021, 12:57pm UTC](https://discourse.cakephp.org/t/how-to-pass-exception-message-to-user-rather-than-internal-error/9656/5 "2021-09-03T12:57:39Z")

</div>

> [@raul338](#):
>
> You shouldn’t do that. It can expose info about your database structure, bad actors will use that against your website.

but I want to show them why there is an error 🙂 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.
