Skip to content

Commit e388ee9

Browse files
committed
Added extendable exception codes trait
1 parent a1eae95 commit e388ee9

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Whitecube\LaravelFlexibleContent\Exceptions\Concerns;
4+
5+
trait HasExtendableCodes
6+
{
7+
/**
8+
* Add a new exception code to the existing array
9+
*
10+
* @param int $code
11+
* @param string $message
12+
* @return void
13+
**/
14+
static public function registerCode($code, $message)
15+
{
16+
static::$codes[$code] = $message;
17+
}
18+
19+
/**
20+
* Get message for given exception code
21+
*
22+
* @param int $code
23+
* @return null|string
24+
**/
25+
static public function getCodeMessage($code)
26+
{
27+
return static::$codes[$code] ?? null;
28+
}
29+
}

src/Exceptions/InstanceNotInsertableException.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
class InstanceNotInsertableException extends InvalidArgumentException
99
{
10+
use Concerns\HasExtendableCodes;
11+
1012
/**
1113
* The main refusal reason codes.
1214
*
@@ -20,7 +22,7 @@ class InstanceNotInsertableException extends InvalidArgumentException
2022
*
2123
* @var array
2224
**/
23-
static public $reasons = [
25+
static public $codes = [
2426
1 => 'Limit reached',
2527
2 => 'Layout limit reached',
2628
];
@@ -34,7 +36,7 @@ class InstanceNotInsertableException extends InvalidArgumentException
3436
*/
3537
static public function make(Layout $instance, $code) : InvalidArgumentException
3638
{
37-
$reason = static::$reasons[$code] ?? 'Unknown refusal reason';
39+
$reason = static::getCodeMessage($code) ?? 'Unknown refusal reason';
3840

3941
return new static($reason . ': Could not insert instance "' . $instance->getId() . '" with key "' . $instance->getKey() . '".', $code);
4042
}

0 commit comments

Comments
 (0)