PHP code example of wikimedia / normalized-exception

1. Go to this page and download the library: Download wikimedia/normalized-exception library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

wikimedia / normalized-exception example snippets


use Wikimedia\NormalizedException\NormalizedException;

throw new NormalizedException( 'Invalid value: {value}', [ 'value' => $value ] );

use Wikimedia\NormalizedException\INormalizedException;

try {
	mightThrow();
} catch ( INormalizedException $e ) {
	$psr3Logger->error( $e->getNormalizedMessage(), $e->getMessageContext() );
	echo 'Error: ' . $e->getMessage();
}

use Exception
use Wikimedia\NormalizedException\INormalizedException;
use Wikimedia\NormalizedException\NormalizedExceptionTrait;

class MyException extends Exception implements INormalizedException {
	use NormalizedExceptionTrait {
		NormalizedExceptionTrait::normalizedConstructor as __construct;
	}
}

throw new MyException( 'Invalid value!' );
throw new MyException( 'Invalid value: {value}', [ 'value' => $value ] );
throw new MyException( 'Invalid value: {value}', [ 'value' => $value ], /* code */ -1, $previous );

use Wikimedia\NormalizedException\INormalizedException;
use Wikimedia\NormalizedException\NormalizedExceptionTrait;

class MyException extends SomeException implements INormalizedException {
	use NormalizedExceptionTrait;

	public function __construct(
		string $normalizedMessage,
		array $messageContext = [],
		$otherParam1,
		$otherParam2
	) {
		// these properties are defined by the trait and must be set
		$this->normalizedMessage = $normalizedMessage;
		$this->messageContext = $messageContext;

		// replaces the PSR-3 tokens
		$message = self::getMessageFromNormalizedMessage( $normalizedMessage, $messageContext );

		parent::__construct( $message, $otherParam1, $otherParam2 );
	}
}