Skip to content

Commit 33c9e09

Browse files
author
Maxim Lanin
committed
Fix property conflict; Add ShowsTrace contract
1 parent f1203a0 commit 33c9e09

File tree

6 files changed

+55
-19
lines changed

6 files changed

+55
-19
lines changed

src/ApiException.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Contracts\Support\Arrayable;
66
use Illuminate\Contracts\Support\Jsonable;
7+
use Lanin\Laravel\ApiExceptions\Contracts\ShowsTrace;
78
use Symfony\Component\Debug\Exception\FlattenException;
89

910
abstract class ApiException extends IdException implements Jsonable, \JsonSerializable, Arrayable
@@ -79,7 +80,7 @@ public function toArray()
7980
}
8081
}
8182

82-
if (env('APP_DEBUG')) {
83+
if (env('APP_DEBUG') && $this instanceof ShowsTrace) {
8384
$return['trace'] = FlattenException::create($e)->getTrace();
8485
}
8586

src/Contracts/ShowsTrace.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Lanin\Laravel\ApiExceptions\Contracts;
4+
5+
interface ShowsTrace
6+
{
7+
8+
}

src/ExceptionHandlerTrait.php

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,12 @@
66
use Illuminate\Auth\Access\AuthorizationException;
77
use Illuminate\Auth\AuthenticationException;
88
use Illuminate\Database\Eloquent\ModelNotFoundException;
9-
use Illuminate\Session\TokenMismatchException;
109
use Illuminate\Validation\ValidationException;
11-
use Symfony\Component\HttpKernel\Exception\HttpException;
1210
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
1311
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
1412

1513
trait ExceptionHandlerTrait
1614
{
17-
/**
18-
* A list of the exception types that should not be reported.
19-
*
20-
* @var array
21-
*/
22-
protected $dontReport = [
23-
AuthenticationException::class,
24-
AuthorizationException::class,
25-
HttpException::class,
26-
ModelNotFoundException::class,
27-
TokenMismatchException::class,
28-
ValidationException::class,
29-
Contracts\DontReport::class,
30-
];
31-
3215
/**
3316
* Report or log an exception.
3417
*

src/InternalServerErrorApiException.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
namespace Lanin\Laravel\ApiExceptions;
44

55
use Exception;
6+
use Lanin\Laravel\ApiExceptions\Contracts\ShowsTrace;
67

7-
class InternalServerErrorApiException extends ApiException
8+
class InternalServerErrorApiException extends ApiException implements ShowsTrace
89
{
910
/**
1011
* @param string $message

src/LaravelExceptionHandler.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,30 @@
22

33
namespace Lanin\Laravel\ApiExceptions;
44

5+
use Illuminate\Auth\Access\AuthorizationException;
6+
use Illuminate\Auth\AuthenticationException;
7+
use Illuminate\Database\Eloquent\ModelNotFoundException;
58
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
9+
use Illuminate\Foundation\Testing\HttpException;
10+
use Illuminate\Session\TokenMismatchException;
11+
use Illuminate\Validation\ValidationException;
612

713
class LaravelExceptionHandler extends ExceptionHandler
814
{
915
use ExceptionHandlerTrait;
16+
17+
/**
18+
* A list of the exception types that should not be reported.
19+
*
20+
* @var array
21+
*/
22+
protected $dontReport = [
23+
AuthenticationException::class,
24+
AuthorizationException::class,
25+
HttpException::class,
26+
ModelNotFoundException::class,
27+
TokenMismatchException::class,
28+
ValidationException::class,
29+
Contracts\DontReport::class,
30+
];
1031
}

src/LumenExceptionHandler.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,31 @@
22

33
namespace Lanin\Laravel\ApiExceptions;
44

5+
use Illuminate\Auth\Access\AuthorizationException;
6+
use Illuminate\Auth\AuthenticationException;
7+
use Illuminate\Database\Eloquent\ModelNotFoundException;
8+
use Illuminate\Foundation\Testing\HttpException;
9+
use Illuminate\Session\TokenMismatchException;
10+
use Illuminate\Validation\ValidationException;
511
use Laravel\Lumen\Exceptions\Handler as ExceptionHandler;
612

13+
714
class LumenExceptionHandler extends ExceptionHandler
815
{
916
use ExceptionHandlerTrait;
17+
18+
/**
19+
* A list of the exception types that should not be reported.
20+
*
21+
* @var array
22+
*/
23+
protected $dontReport = [
24+
AuthenticationException::class,
25+
AuthorizationException::class,
26+
HttpException::class,
27+
ModelNotFoundException::class,
28+
TokenMismatchException::class,
29+
ValidationException::class,
30+
Contracts\DontReport::class,
31+
];
1032
}

0 commit comments

Comments
 (0)