libasynql  3.2.0
Asynchronous MySQL access library for PocketMine plugins.
SqlError.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * libasynql
5  *
6  * Copyright (C) 2018 SOFe
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 declare(strict_types=1);
22 
23 namespace poggit\libasynql;
24 
25 use Closure;
26 use Exception;
27 use ReflectionClass;
30 use function get_class;
31 use function get_resource_type;
32 use function is_object;
33 use function is_resource;
34 use function json_encode;
35 use function sprintf;
36 
40 class SqlError extends RuntimeException{
44  public const STAGE_CONNECT = "CONNECT";
48  public const STAGE_PREPARE = "PREPARE";
52  public const STAGE_EXECUTE = "EXECUTION";
56  public const STAGE_RESPONSE = "RESPONSE";
57 
58  private $stage;
59  private $errorMessage;
60  private $query;
61  private $args;
62 
63  public function __construct(string $stage, string $errorMessage, string $query = null, array $args = null){
64  $this->stage = $stage;
65  $this->errorMessage = $errorMessage;
66  $this->query = $query;
67  $this->args = $args;
68 
69  parent::__construct("SQL $stage error: $errorMessage" . ($query === null ? "" : (", for query $query | " . json_encode($args))));
70  $this->flattenTrace();
71  }
72 
78  public function getStage() : string{
79  return $this->stage;
80  }
81 
87  public function getErrorMessage() : string{
88  return $this->errorMessage;
89  }
90 
96  public function getQuery() : ?string{
97  return $this->query;
98  }
99 
105  public function getArgs() : ?array{
106  return $this->args;
107  }
108 
114  protected function flattenTrace() : void{
115  $traceProperty = (new ReflectionClass(Exception::class))->getProperty('trace');
116  $traceProperty->setAccessible(true);
117  $flatten = function(&$value){
118  if($value instanceof Closure){
119  $closureReflection = new ReflectionFunction($value);
120  $value = sprintf(
121  '(Closure at %s:%s)',
122  $closureReflection->getFileName(),
123  $closureReflection->getStartLine()
124  );
125  }elseif(is_object($value)){
126  $value = sprintf('object(%s)', get_class($value));
127  }elseif(is_resource($value)){
128  $value = sprintf('resource(%s)', get_resource_type($value));
129  }
130  };
131  do{
132  $trace = $traceProperty->getValue($this);
133  foreach($trace as &$call){
134  array_walk_recursive($call['args'], $flatten);
135  }
136  unset($call);
137  $traceProperty->setValue($this, $trace);
138  }while($exception = $this->getPrevious());
139  $traceProperty->setAccessible(false);
140  }
141 }
__construct(string $stage, string $errorMessage, string $query=null, array $args=null)
Definition: SqlError.php:63