Comments (5)
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.
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.
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.
thank you so much
from slim4-skeleton.
from slim4-skeleton.
Related Issues (20)
- Install troubleshooting HOT 2
- Wrong statement in the configuration doc page HOT 4
- Database migrations with phinx and schema.sql generation HOT 5
- Which session package should I use? HOT 4
- Missing TemplateRenderer/HtmlRenderer HOT 2
- Question: MVC vs ADR Pattern HOT 2
- Link in docs is wrong HOT 1
- logger test's settings are useless HOT 4
- How to use loggerFactory in Routing/JwtAuth
- Using skeleton in a subdirectory HOT 2
- Error add slim/twig-view (composer require slim/twig-view) HOT 2
- Cannot install csfr without using downgrade HOT 2
- Problem getting JWT to work as per your eBook HOT 6
- Undefined function 'uuid_create' in LoggerFactory HOT 2
- 404 in /docs/configuration.md HOT 2
- How to handle data from repository to presentation as JSON for REST API HOT 4
- Add demonstration URL HOT 5
- Letting the bin/console.php setup command have non-interactive mode HOT 5
- Any auth in your sceleton? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from slim4-skeleton.