2

I am using CakePHP and sometimes something goes wrong and I get a wall of error text.

E.g.

$response = array(
            'error'=>true,
            'invalidKey'=>false,
            'message'=>ERROR_ACCOUNT_EXISTS,
            );

I have this code and let's say ERROR_ACCOUNT_EXISTS is not defined anywhere. It should throw an exception.

Here is what I get:

<pre class="cake-error"><a href="javascript:void(0);" onclick="document.getElementById('cakeErr55ad1d63b99ac-trace').style.display = (document.getElementById('cakeErr55ad1d63b99ac-trace').style.display == 'none' ? '' : 'none');"><b>Notice</b> (8)</a>: Use of undefined constant MESSAGE_REGISTRATION_SUCCESSFUL - assumed 'MESSAGE_REGISTRATION_SUCCESSFUL' [<b>APP/Controller/ApiController.php</b>, line <b>56</b>]<div id="cakeErr55ad1d63b99ac-trace" class="cake-stack-trace" style="display: none;"><a href="javascript:void(0);" onclick="document.getElementById('cakeErr55ad1d63b99ac-code').style.display = (document.getElementById('cakeErr55ad1d63b99ac-code').style.display == 'none' ? '' : 'none')">Code</a> <a href="javascript:void(0);" onclick="document.getElementById('cakeErr55ad1d63b99ac-context').style.display = (document.getElementById('cakeErr55ad1d63b99ac-context').style.display == 'none' ? '' : 'none')">Context</a><pre id="cakeErr55ad1d63b99ac-code" class="cake-code-dump" style="display: none;"><code><span style="color: #000000"><span style="color: #0000BB">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'invalidKey'</span><span style="color: #007700">=&gt;</span><span style="color: #0000BB">false</span><span style="color: #007700">,</span></span></code><code><span style="color: #000000"><span style="color: #0000BB">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'user'</span><span style="color: #007700">=&gt;</span><span style="color: #0000BB">$result</span><span style="color: #007700">[</span><span style="color: #DD0000">'User'</span><span style="color: #007700">],</span></span></code><span class="code-highlight"><code><span style="color: #000000"><span style="color: #0000BB">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'message'</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">MESSAGE_REGISTRATION_SUCCESSFUL</span><span style="color: #007700">,</span></span></code></span></pre><pre id="cakeErr55ad1d63b99ac-context" class="cake-context" style="display: none;">$name = &#039;

I do not want this kind of html error.

I have tried try catch clause but the result is the same.

How can I handle such errors?

Moe Far
  • 2,742
  • 2
  • 23
  • 41
Muhammad Umar
  • 11,391
  • 21
  • 91
  • 193
  • I m not sure if I understand your problem. Is you problem, that you want to throw a special exception? If yes why not say it so? – Calamity Jane Jul 20 '15 at 20:55
  • AFAIK, this yield a HTML `500` status, so you can handle it like you'd handle any `500` (http://book.cakephp.org/2.0/en/development/exceptions.html#exception-renderer). These new pages will only show when debug is set to `0`. – Holt Jul 20 '15 at 21:11

1 Answers1

-1
ERROR_ACCOUNT_EXISTS

This is a constant and if not defined will cause a fatal error which is not caught it seems. See How do I catch a PHP Fatal Error

Also this smells like a pretty bad use of constants, they should not be used for messages. Technically possible? Yes. Good practice? No. Use the translation functions of CakePHP instead or just a sting in place if no translation is required.

So instead of catching the fatal error I would fix the problem in the first place by not using a constant here - or define the constant(s) somewhere. Fatal errors should fixed and not hidden and worked around. Not fixing fatals and even notices is like putting some colour above the rotten core and considered as bad practice.

Community
  • 1
  • 1
floriank
  • 25,546
  • 9
  • 42
  • 66
  • that wasn't my question, yes i know the problem and i could have fixed the problem, however i am asking what is causing this wall of text and how can catch it and not send it if any unusual error occur. the exception will most probably cause my app to crash and to avoid it i have to catch this error. Also, i can't hardcode strings because each string can be used at a lot of places. What should be good practise here? – Muhammad Umar Jul 21 '15 at 17:22
  • I completely disagree. Seriously asking what the cause is? **Wrong code**. Fix it. From the php documentation: "These indicate errors that *can not be recovered from*, such as a memory allocation problem. *Execution of the script is halted.*" It simply should not happen under any circumstances. Especially the given one is such an obvious issue and easy to fix: Declare your constants. – floriank Jul 21 '15 at 22:07
  • yes i will AND HAVE removed the variable, but it was just for sample. However i asked a simple question, how can i handle the damn exception, i don't want it to happen for any other problem it may cause entire app to malfunction. – Muhammad Umar Jul 22 '15 at 04:53