![]() |
CI4: Handling model errors - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: Model-View-Controller (https://forum.codeigniter.com/forumdisplay.php?fid=10) +--- Thread: CI4: Handling model errors (/showthread.php?tid=76153) |
CI4: Handling model errors - rmilecki - 04-17-2020 I'm a bit confused on how to properly handle errors that happen in a model:
RE: CI4: Handling model errors - InsiteFX - 04-18-2020 Exceptions are unrecoverable errors. To get any database query errors use the below. PHP Code: $error = $db->error(); // Has keys 'code' and 'message' RE: CI4: Handling model errors - joho - 08-21-2023 (04-18-2020, 03:26 AM)InsiteFX Wrote: Exceptions are unrecoverable errors. They may be unrecoverable, but through decent exception handling, shouldn't you be able to at least exit gracefully? I've run into a TypeError exception, thrown in CI's BaseModel-file/class, but the exception never bubbles to my own exception handler in the code (which is wrapped around a $mymodel->save() call). Instead the application is terminated and I never receive control back to my (cleanup) code. Hmm ... I just realized TypeError is not actually an exception per se. Re-writing the try {} catch {} block to use \TypeError instead, it actually does get caught and the "exception" handler is invoked. Maybe the documentation should mention this in the "Error Handling" section? RE: CI4: Handling model errors - kenjis - 08-21-2023 (04-17-2020, 03:06 AM)rmilecki Wrote: As CodeIgniter4 seems to mix two ways of handling errors should my controller always do something like: Yes. This is not good design. But if you use Model, you should write code like that. Or change all the errors that the Model returns false to an Exception by yourself. In Shield, we do like that. See https://github.com/codeigniter4/shield/blob/develop/src/Models/CheckQueryReturnTrait.php RE: CI4: Handling model errors - InsiteFX - 08-21-2023 You do know this post is 3 years old. RE: CI4: Handling model errors - joho - 08-22-2023 I do, but it's still valid, no? Or has something changed in CI4 that makes this easier? ? RE: CI4: Handling model errors - kenjis - 08-22-2023 @joho See https://www.php.net/manual/en/class.typeerror.php TypeError extends Error, not Exception. RE: CI4: Handling model errors - joho - 08-22-2023 (08-22-2023, 01:06 AM)kenjis Wrote: @joho See https://www.php.net/manual/en/class.typeerror.php Yes, that's what I wrote in my follow-up post ![]() It makes it hard to write a "catch-all" handler though, when the library/framework code throws different types of errors/exceptions depending on what happens. RE: CI4: Handling model errors - kenjis - 08-22-2023 You can catch Throwable: https://www.php.net/manual/en/class.throwable.php RE: CI4: Handling model errors - joho - 08-22-2023 (08-22-2023, 01:35 AM)kenjis Wrote: You can catch Throwable: That's great! Maybe something could be put in the CI documentation for this. I consider graceful error handling to be one of the most important aspects of any proper application. |