GithubHelp home page GithubHelp logo

Comments (5)

odan avatar odan commented on June 18, 2024

I handle this by catching the exception. For example, when I start a database transaction, I roll it back if an exception occurs.

See here: Transaction

To catch and render all other kind of (database and application) errors, I configured the Slim ErrorMiddleware to use a custom DefaultErrorHandler that renders the Exception into a proper HTTP JSON response.

https://github.com/odan/slim4-skeleton/blob/master/src/Handler/DefaultErrorHandler.php
https://github.com/odan/slim4-skeleton/blob/master/config/middleware.php

from slim4-skeleton.

well-it-wasnt-me avatar well-it-wasnt-me commented on June 18, 2024

yes im using the transactions.

and the try/catch too.

but in this case the insert doesnt trigger the exception but yet its not done.

try {
          $id_dog = $this->queryFactory->newInsert('dogs', $row)->execute()->lastInsertId();
          // THIS GET CALLED
          if (!$id_dogs) {
              $this->transaction->rollback();
              return ['status' => 'error', 'message' => Definition::ERR_ADD_DOG];
          }

      } catch (\Exception $e){
         // THIS NOT
          echo $e->getMessage();
      }

how can i "debug" this and understand why it fails ?

from slim4-skeleton.

odan avatar odan commented on June 18, 2024

It looks like the lastInsertId returns a new ID. So the query might work.

Make sure to enable exception for the database connection.

PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,

You also don't need to check for !$id_dogs then because if something was wrong, the Exception should be thrown anyway.

Also make sure to commit the transaction. Otherwise the insert command changes nothing and the transaction will be reverted at the end.

Example:

use Exception;
// ...

try {
    $this->queryFactory->newInsert('dogs', $row)->execute();
    $this->transaction->commit(); // <--- this is needed
} catch (Exception $e){
    $this->transaction->rollback();
    // Optional log error here
    // ...
}

Edit: Note that a transaction makes only sense when you have a bunch of queries with multiple read and write operations.

from slim4-skeleton.

well-it-wasnt-me avatar well-it-wasnt-me commented on June 18, 2024

thank you so much

from slim4-skeleton.

well-it-wasnt-me avatar well-it-wasnt-me commented on June 18, 2024

from slim4-skeleton.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.