gestion d'erreur par exception
This commit is contained in:
parent
72e12e17c0
commit
23fdca6c52
@ -106,9 +106,17 @@ class JsonDb extends \Prowebcraft\Dot
|
|||||||
if (!file_exists($this->db)) {
|
if (!file_exists($this->db)) {
|
||||||
return null;
|
return null;
|
||||||
} else {
|
} else {
|
||||||
$this->data = json_decode(file_get_contents($this->db), true);
|
// 3 essais
|
||||||
|
for($i = 0; $i <3; $i++) {
|
||||||
|
if ($this->data = json_decode(@file_get_contents($this->db), true) ) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
// Pause de 10 millisecondes
|
||||||
|
usleep(10000);
|
||||||
|
}
|
||||||
|
// Gestion de l'erreur
|
||||||
if (!$this->data === null) {
|
if (!$this->data === null) {
|
||||||
throw new \InvalidArgumentException('Erreur de lecture du fichier de données ' . $this->db);
|
throw new \Exception('Erreur de lecture du fichier de données ' . $this->db);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -120,14 +128,14 @@ class JsonDb extends \Prowebcraft\Dot
|
|||||||
*/
|
*/
|
||||||
public function save() {
|
public function save() {
|
||||||
// 3 essais
|
// 3 essais
|
||||||
for($i = 0; $i <=3; $i++) {
|
for($i = 0; $i <3; $i++) {
|
||||||
if(file_put_contents($this->db, json_encode($this->data, JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT|LOCK_EX)) !== false) {
|
if(@file_put_contents($this->db, json_encode($this->data, JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT|LOCK_EX)) !== false) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
// Pause de 10 millisecondes
|
// Pause de 10 millisecondes
|
||||||
usleep(10000);
|
usleep(10000);
|
||||||
if ($i === 3) {
|
if ($i === 2) {
|
||||||
throw new \InvalidArgumentException('Erreur d\'écriture du fichier de données ' . $this->db);
|
throw new \Exception('Erreur d\'écriture du fichier de données ' . $this->db );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user