Welcome Guest, Not a member yet? Register   Sign In
CI4: Handling model errors
#1

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.
Reply


Messages In This Thread
CI4: Handling model errors - by rmilecki - 04-17-2020, 03:06 AM
RE: CI4: Handling model errors - by InsiteFX - 04-18-2020, 03:26 AM
RE: CI4: Handling model errors - by joho - 08-21-2023, 01:27 AM
RE: CI4: Handling model errors - by kenjis - 08-21-2023, 06:42 PM
RE: CI4: Handling model errors - by InsiteFX - 08-21-2023, 08:48 PM
RE: CI4: Handling model errors - by joho - 08-22-2023, 12:01 AM
RE: CI4: Handling model errors - by kenjis - 08-22-2023, 01:06 AM
RE: CI4: Handling model errors - by joho - 08-22-2023, 01:18 AM
RE: CI4: Handling model errors - by kenjis - 08-22-2023, 01:35 AM
RE: CI4: Handling model errors - by joho - 08-22-2023, 02:26 AM



Theme © iAndrew 2016 - Forum software by © MyBB