Skip to content

Commit

Permalink
Wrap exception in first party exception
Browse files Browse the repository at this point in the history
  • Loading branch information
timacdonald committed Jan 11, 2023
1 parent 3ab9814 commit 50889d3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
8 changes: 7 additions & 1 deletion src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use BackedEnum;
use Brick\Math\BigDecimal;
use Brick\Math\Exception\MathException as BrickMathException;
use Brick\Math\RoundingMode;
use Carbon\CarbonImmutable;
use Carbon\CarbonInterface;
Expand All @@ -25,6 +26,7 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection as BaseCollection;
use Illuminate\Support\Exceptions\MathException;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -1322,7 +1324,11 @@ public function fromFloat($value)
*/
protected function asDecimal($value, $decimals)
{
return (string) BigDecimal::of($value)->toScale($decimals, RoundingMode::HALF_UP);
try {
return (string) BigDecimal::of($value)->toScale($decimals, RoundingMode::HALF_UP);
} catch (BrickMathException $e) {
throw new MathException('Unable to cast value to a decimal.', previous: $e);
}
}

/**
Expand Down
10 changes: 10 additions & 0 deletions src/Illuminate/Support/Exceptions/MathException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Illuminate\Support\Exceptions;

use RuntimeException;

class MathException extends RuntimeException
{
//
}
15 changes: 10 additions & 5 deletions tests/Integration/Database/EloquentModelDecimalCastingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Brick\Math\Exception\NumberFormatException;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Exceptions\MathException;
use Illuminate\Support\Facades\Schema;
use Illuminate\Tests\Integration\Database\DatabaseTestCase;

Expand Down Expand Up @@ -52,7 +53,7 @@ public function testItHandlesIntegersWithUnderscores()
$this->assertSame('1234.50', $model->amount);
}

public function testItThrowsOnNonNumericValues()
public function testItWrapsThrownExceptions()
{
$model = new class extends Model
{
Expand All @@ -64,10 +65,14 @@ public function testItThrowsOnNonNumericValues()
};
$model->amount = 'foo';

$this->expectException(NumberFormatException::class);
$this->expectExceptionMessage('The given value "foo" does not represent a valid number.');

$model->amount;
try {
$model->amount;
$this->fail();
} catch (MathException $e) {
$this->assertSame('Unable to cast value to a decimal.', $e->getMessage());
$this->assertInstanceOf(NumberFormatException::class, $e->getPrevious());
$this->assertSame('The given value "foo" does not represent a valid number.', $e->getPrevious()->getMessage());
}
}

public function testItHandlesMissingIntegers()
Expand Down

0 comments on commit 50889d3

Please sign in to comment.