CodeIgniter Forums
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:
  • According to the "Error Handling" part of documentation model should throw an exception on error
  • CodeIgniter4 in its Model returns false without throwing an exception on validation error
So I guess my questions are:
  1. When Model should simply return false and when should it throw an exception?
  2. If I decide to return false in my custom model method, can I make my custom error message available over Model:errors() for a controller?
  3. As CodeIgniter4 seems to mix two ways of handling errors should my controller always do something like:
    try {    $post = $postModel->insertWithAttachments($data, $attachments);} catch (\Exception $e) {    return redirect()->back()->withInput()->with('errors', $e->getMessage());}if (!$post) {    return redirect()->back()->withInput()->with('errors', $postModel->errors());}This seems a bit complex as for a single model call.



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.

To get any database query errors use the below.

PHP Code:
$error $db->error(); // Has keys 'code' and 'message' 


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:

PHP Code:
try {
    $post $postModel->insertWithAttachments($data$attachments);
} catch (\
Exception $e) {
    return redirect()->back()->withInput()->with('errors'$e->getMessage());
}

if (!
$post) {
    return redirect()->back()->withInput()->with('errors'$postModel->errors());


This seems a bit complex as for a single model call.

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
TypeError extends Error, not Exception.

Yes, that's what I wrote in my follow-up post Big Grin 

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:
https://www.php.net/manual/en/class.throwable.php

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.