Skip to content
This repository was archived by the owner on Dec 10, 2024. It is now read-only.

Commit f77461f

Browse files
committed
added Universal Unique Identifier For errors.
1 parent dde18db commit f77461f

File tree

12 files changed

+135
-4
lines changed

12 files changed

+135
-4
lines changed

src/Error.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* For the full copyright and license information, please view the LICENSE
88
* file that was distributed with this source code.
99
*/
10+
1011
namespace arabcoders\errors;
1112

1213
use arabcoders\errors\Interfaces\ErrorInterface;
@@ -262,6 +263,7 @@ public function handleError( ErrorMapInterface $error ) : ErrorInterface
262263
->setTrace( $this->tracer->setIgnore( [ __FILE__ ] )->process()->getTrace() )
263264
->setMessage( $this->formatter->formatError( $error ) )
264265
->setError( $error )
266+
->setId( $this->createUUID() )
265267
->getInstance();
266268

267269
if ( array_key_exists( $error->getNumber(), $this->listener ) )
@@ -292,6 +294,7 @@ public function handleException( \Throwable $exception ) : ErrorInterface
292294
->setStructured( $this->getStructured()->setException( $exception )->process()->getStructured() )
293295
->setTrace( $this->tracer->setContext( $exception->getTrace() )->setIgnore( [ __FILE__ ] )->process()->getTrace() )
294296
->setMessage( $this->formatter->formatException( $exception ) )
297+
->setId( $this->createUUID() )
295298
->getInstance();
296299

297300
$name = get_class( $exception );
@@ -551,4 +554,34 @@ protected function setupDefault() : ErrorInterface
551554
return $this;
552555
}
553556

557+
/**
558+
* Creates Universal Unique Identifier For Error.
559+
*
560+
* Implements UUIDv4.
561+
*
562+
* @return string
563+
*/
564+
protected function createUUID() : string
565+
{
566+
$version = 4;
567+
568+
$hash = bin2hex( random_bytes( 16 ) );
569+
570+
return sprintf(
571+
'%08s-%04s-%04x-%04x-%12s',
572+
// 32 bits for "time_low"
573+
substr( $hash, 0, 8 ),
574+
// 16 bits for "time_mid"
575+
substr( $hash, 8, 4 ),
576+
// 16 bits for "time_hi_and_version",
577+
// four most significant bits holds version number
578+
( hexdec( substr( $hash, 12, 4 ) ) & 0x0fff ) | $version << 12,
579+
// 16 bits, 8 bits for "clk_seq_hi_res",
580+
// 8 bits for "clk_seq_low",
581+
// two most significant bits holds zero and one for variant DCE1.1
582+
( hexdec( substr( $hash, 16, 4 ) ) & 0x3fff ) | 0x8000,
583+
// 48 bits for "node"
584+
substr( $hash, 20, 12 )
585+
);
586+
}
554587
}

src/Interfaces/MapInterface.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* For the full copyright and license information, please view the LICENSE
88
* file that was distributed with this source code.
99
*/
10+
1011
namespace arabcoders\errors\Interfaces;
1112

1213
/**
@@ -62,6 +63,22 @@ public function setTrace( array $trace ) : MapInterface;
6263
*/
6364
public function getTrace() : array;
6465

66+
/**
67+
* Set Unique Id for error.
68+
*
69+
* @param string $id
70+
*
71+
* @return MapInterface
72+
*/
73+
public function setId( string $id ) : MapInterface;
74+
75+
/**
76+
* Get Unique Error Id.
77+
*
78+
* @return string
79+
*/
80+
public function getId() : string;
81+
6582
/**
6683
* Set message.
6784
*

src/Logging/File.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* For the full copyright and license information, please view the LICENSE
88
* file that was distributed with this source code.
99
*/
10+
1011
namespace arabcoders\errors\Logging;
1112

1213
use arabcoders\errors\Interfaces\MapInterface;
@@ -48,7 +49,7 @@ public function __construct( $file, string $mode = 'ab' )
4849
*/
4950
public function process() : LoggingInterface
5051
{
51-
$message = trim( '[' . gmdate( \DateTime::W3C ) . '] ' . $this->getMap()->getMessage() );
52+
$message = trim( '[' . gmdate( \DateTime::W3C ) . '][' . $this->getMap()->getId() . '] ' . $this->getMap()->getMessage() );
5253
$trace = $this->getMap()->getTrace();
5354

5455
if ( $trace )

src/Logging/Remote.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* For the full copyright and license information, please view the LICENSE
88
* file that was distributed with this source code.
99
*/
10+
1011
namespace arabcoders\errors\Logging;
1112

1213
use arabcoders\errors\Interfaces\MapInterface;
@@ -60,6 +61,7 @@ public function process() : LoggingInterface
6061
{
6162
$this->async[] = $this->request->requestAsync( 'post', $this->server, [
6263
'form_params' => [
64+
'id' => $this->getMap()->getId(),
6365
'message' => $this->getMap()->getMessage(),
6466
'trace' => json_encode( $this->getMap()->getTrace() ),
6567
'structured' => json_encode( $this->getMap()->getStructured() ),

src/Logging/Syslog.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* For the full copyright and license information, please view the LICENSE
88
* file that was distributed with this source code.
99
*/
10+
1011
namespace arabcoders\errors\Logging;
1112

1213
use arabcoders\errors\Interfaces\ErrorInterface;
@@ -81,6 +82,11 @@ public function process() : LoggingInterface
8182

8283
$message = $this->getMap()->getMessage();
8384

85+
if ( $this->getMap()->getId() )
86+
{
87+
$message .= ' REF [ ' . $this->getMap()->getId() . ' ]';
88+
}
89+
8490
if ( $this->getMap()->getStructured() )
8591
{
8692
$message .= PHP_EOL . print_r( $this->getMap()->getStructured(), true );

src/Map.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* For the full copyright and license information, please view the LICENSE
88
* file that was distributed with this source code.
99
*/
10+
1011
namespace arabcoders\errors;
1112

1213
use arabcoders\errors\Interfaces\ErrorInterface;
@@ -35,6 +36,11 @@ class Map implements MapInterface
3536
*/
3637
protected $message = '';
3738

39+
/**
40+
* @var string Unique Error id.
41+
*/
42+
protected $id = '';
43+
3844
/**
3945
* @var int Error type {@see ErrorInterface::TYPE_ERROR} or {@see ErrorInterface::TYPE_EXCEPTION}
4046
*/
@@ -118,6 +124,30 @@ public function getTrace() : array
118124
return $this->trace;
119125
}
120126

127+
/**
128+
* Set Unique Id for error.
129+
*
130+
* @param string $id
131+
*
132+
* @return MapInterface
133+
*/
134+
public function setId( string $id ) : MapInterface
135+
{
136+
$this->id = $id;
137+
138+
return $this;
139+
}
140+
141+
/**
142+
* Get Unique Error Id.
143+
*
144+
* @return string
145+
*/
146+
public function getId() : string
147+
{
148+
return $this->id;
149+
}
150+
121151
/**
122152
* Set message.
123153
*
@@ -264,6 +294,7 @@ public function clear() : MapInterface
264294
$this->trace = [];
265295
$this->structured = [];
266296
$this->message = '';
297+
$this->id = '';
267298
$this->type = 0;
268299
$this->errorMap = null;
269300
$this->exceptionMap = null;

src/Output/Basic.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* For the full copyright and license information, please view the LICENSE
88
* file that was distributed with this source code.
99
*/
10+
1011
namespace arabcoders\errors\Output;
1112

1213
use arabcoders\errors\Interfaces\MapInterface;
@@ -43,6 +44,11 @@ public function display() : OutputInterface
4344

4445
print self::MSG;
4546

47+
if ( $this->getMap()->getId() )
48+
{
49+
echo PHP_EOL . sprintf( 'Unique Error Id: %s', $this->getMap()->getId() );
50+
}
51+
4652
return $this;
4753
}
4854

src/Output/CLI.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* For the full copyright and license information, please view the LICENSE
88
* file that was distributed with this source code.
99
*/
10+
1011
namespace arabcoders\errors\Output;
1112

1213
use arabcoders\errors\Interfaces\MapInterface;
@@ -36,7 +37,16 @@ public function display() : OutputInterface
3637
define( 'STDERR', fopen( 'php://stderr', 'w' ) );
3738
}
3839

39-
fwrite( STDERR, $this->getMap()->getMessage() . PHP_EOL );
40+
$message = $this->getMap()->getMessage();
41+
42+
if ( $this->getMap()->getId() )
43+
{
44+
$message .= sprintf( ' - REF [ %s ]', $this->getMap()->getId() );
45+
}
46+
47+
$message .= PHP_EOL;
48+
49+
fwrite( STDERR, $message );
4050

4151
return $this;
4252
}

src/Output/HTML.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* For the full copyright and license information, please view the LICENSE
88
* file that was distributed with this source code.
99
*/
10+
1011
namespace arabcoders\errors\Output;
1112

1213
use arabcoders\errors\Interfaces\ErrorInterface;
@@ -42,6 +43,7 @@ public function display() : OutputInterface
4243
$string .= '<title>Error Page</title>';
4344
$string .= '<meta name="viewport" content="width=device-width, initial-scale=1.0"></head><body>';
4445
$string .= '<h1>An %s has Occurred</h1><blockquote>%s</blockquote>%s%s';
46+
$string .= '<h2>Unique Error ID</h2><blockquote>%s</blockquote>';
4547
$string .= '</body></html>';
4648

4749
if ( $this->getMap()->getTrace() )
@@ -72,7 +74,7 @@ public function display() : OutputInterface
7274

7375
$type = ( ErrorInterface::TYPE_ERROR === $this->getMap()->getType() ) ? 'Error' : 'Exception';
7476

75-
print sprintf( $string, $type, $this->escapeString( $this->getMap()->getMessage() ), $structured, $trace );
77+
print sprintf( $string, $type, $this->escapeString( $this->getMap()->getMessage() ), $structured, $trace, $this->getMap()->getId() );
7678

7779
return $this;
7880
}

src/Output/Twig.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* For the full copyright and license information, please view the LICENSE
88
* file that was distributed with this source code.
99
*/
10+
1011
namespace arabcoders\errors\Output;
1112

1213
use arabcoders\errors\Interfaces\ErrorInterface;
@@ -80,6 +81,7 @@ public function display() : OutputInterface
8081
'className' => $type ?? null,
8182
'type' => $this->getMap()->getType(),
8283
'message' => $this->getMap()->getMessage(),
84+
'UNIQUE_ID' => $this->getMap()->getId(),
8385
'trace' => ( !empty( $trace ) ) ? json_encode( $trace, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT ) : null,
8486
'structured' => ( !empty( $structured ) ) ? json_encode( $structured, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT ) : null,
8587
] );

0 commit comments

Comments
 (0)