Completed
Push — master ( 36a006...f90c70 )
by CodexShaper
04:12
created

Handler::get_http_exception_view()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * The file handle the errors.
4
 *
5
 * @link       https://github.com/maab16
6
 * @since      1.0.0
7
 *
8
 * @package    WPB
9
 * @subpackage WPB/src/Exceptions
10
 */
11
12
namespace WPB\Exceptions;
13
14
use Exception;
15
use Illuminate\Auth\Access\AuthorizationException;
0 ignored issues
show
Bug introduced by
The type Illuminate\Auth\Access\AuthorizationException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use Illuminate\Auth\AuthenticationException;
0 ignored issues
show
Bug introduced by
The type Illuminate\Auth\AuthenticationException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Illuminate\Contracts\Container\BindingResolutionException;
18
use Illuminate\Contracts\Container\Container;
19
use WPB\Contracts\ExceptionHandler as ExceptionHandlerContract;
20
use Illuminate\Contracts\Support\Responsable;
21
use Illuminate\Database\Eloquent\ModelNotFoundException;
22
use Illuminate\Http\Exceptions\HttpResponseException;
23
use Illuminate\Http\JsonResponse;
24
use Illuminate\Http\RedirectResponse;
25
use Illuminate\Http\Response;
26
use Illuminate\Routing\Router;
27
use Illuminate\Session\TokenMismatchException;
28
use Illuminate\Support\Arr;
29
use Illuminate\Support\Facades\Auth;
30
use Illuminate\Support\Facades\View;
31
use Illuminate\Support\ViewErrorBag;
32
use Illuminate\Validation\ValidationException;
0 ignored issues
show
Bug introduced by
The type Illuminate\Validation\ValidationException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
33
use Psr\Log\LoggerInterface;
34
use Symfony\Component\Console\Application as ConsoleApplication;
35
use Symfony\Component\ErrorHandler\ErrorRenderer\HtmlErrorRenderer;
36
use Symfony\Component\HttpFoundation\Exception\SuspiciousOperationException;
37
use Symfony\Component\HttpFoundation\RedirectResponse as SymfonyRedirectResponse;
38
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
39
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
40
use Symfony\Component\HttpKernel\Exception\HttpException;
41
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
42
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
43
use Throwable;
44
use Whoops\Handler\HandlerInterface;
0 ignored issues
show
Bug introduced by
The type Whoops\Handler\HandlerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
45
use Whoops\Run as Whoops;
0 ignored issues
show
Bug introduced by
The type Whoops\Run was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
46
47
/**
48
 * The exception handler.
49
 *
50
 * @since      1.0.0
51
 * @package    WPB
52
 * @subpackage WPB/src/Exceptions
53
 * @author     Md Abu Ahsan basir <[email protected]>
54
 */
55
class Handler implements ExceptionHandlerContract {
56
57
	/**
58
	 * The container implementation.
59
	 *
60
	 * @var \Illuminate\Contracts\Container\Container
61
	 */
62
	protected $container;
63
64
	/**
65
	 * A list of the exception types that are not reported.
66
	 *
67
	 * @var array
68
	 */
69
	protected $dont_report = array();
70
71
	/**
72
	 * A list of the internal exception types that should not be reported.
73
	 *
74
	 * @var array
75
	 */
76
	protected $internal_dont_report = array(
77
		AuthenticationException::class,
78
		AuthorizationException::class,
79
		HttpException::class,
80
		HttpResponseException::class,
81
		ModelNotFoundException::class,
82
		SuspiciousOperationException::class,
83
		TokenMismatchException::class,
84
		ValidationException::class,
85
	);
86
87
	/**
88
	 * A list of the inputs that are never flashed for validation exceptions.
89
	 *
90
	 * @var array
91
	 */
92
	protected $dont_flash = array(
93
		'password',
94
		'password_confirmation',
95
	);
96
97
	/**
98
	 * Create a new exception handler instance.
99
	 *
100
	 * @param \Illuminate\Contracts\Container\Container $container The app container.
101
	 *
102
	 * @return void
103
	 */
104
	public function __construct( Container $container ) {
105
		$this->container = $container;
106
	}
107
108
	/**
109
	 * Report or log an exception.
110
	 *
111
	 * @param \Throwable $e The throwable exception.
112
	 *
113
	 * @throws \Exception Throw the exception.
114
	 *
115
	 * @return void
116
	 */
117
	public function report( Throwable $e ) {
118
		if ( $this->shouldnt_report( $e ) ) {
119
			return;
120
		}
121
122
		$report_callable = array( $e, 'report' );
123
124
		if ( is_callable( $report_callable ) ) {
125
			$this->container->call( $report_callable );
126
127
			return;
128
		}
129
130
		try {
131
			$logger = $this->container->make( LoggerInterface::class );
132
		} catch ( Exception $ex ) {
133
			throw $e;
134
		}
135
136
		$logger->error(
137
			$e->getMessage(),
138
			array_merge(
139
				$this->exception_context( $e ),
140
				$this->context(),
141
				array( 'exception' => $e )
142
			)
143
		);
144
	}
145
146
	/**
147
	 * Determine if the exception should be reported.
148
	 *
149
	 * @param \Throwable $e The throwable exception.
150
	 *
151
	 * @return bool
152
	 */
153
	public function should_report( Throwable $e ) {
154
		return ! $this->shouldnt_report( $e );
155
	}
156
157
	/**
158
	 * Determine if the exception is in the "do not report" list.
159
	 *
160
	 * @param \Throwable $e The throwable exception.
161
	 *
162
	 * @return bool
163
	 */
164
	protected function shouldnt_report( Throwable $e ) {
165
		$dont_report = array_merge( $this->dont_report, $this->internal_dont_report );
166
167
		return ! is_null(
168
			Arr::first(
169
				$dont_report,
170
				function ( $type ) use ( $e ) {
171
					return $e instanceof $type;
172
				}
173
			)
174
		);
175
	}
176
177
	/**
178
	 * Get the default exception context variables for logging.
179
	 *
180
	 * @param \Throwable $e The throwable exception.
181
	 *
182
	 * @return array
183
	 */
184
	protected function exception_context( Throwable $e ) {
0 ignored issues
show
Unused Code introduced by
The parameter $e is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

184
	protected function exception_context( /** @scrutinizer ignore-unused */ Throwable $e ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
185
		return array();
186
	}
187
188
	/**
189
	 * Get the default context variables for logging.
190
	 *
191
	 * @return array
192
	 */
193
	protected function context() {
194
		try {
195
			return array_filter(
196
				array(
197
					'userId' => Auth::id(),
198
				// 'email' => optional(Auth::user())->email,
199
				)
200
			);
201
		} catch ( Throwable $e ) {
202
			return array();
203
		}
204
	}
205
206
	/**
207
	 * Render an exception into an HTTP response.
208
	 *
209
	 * @param \Illuminate\Http\Request $request The app request.
210
	 * @param \Throwable               $e The throwable exception.
211
	 *
212
	 * @throws \Throwable Throw the exception.
213
	 *
214
	 * @return \Symfony\Component\HttpFoundation\Response
215
	 */
216
	public function render( $request, Throwable $e ) {
217
		$response = $e->render( $request );
0 ignored issues
show
Bug introduced by
The method render() does not exist on Throwable. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

217
		/** @scrutinizer ignore-call */ 
218
  $response = $e->render( $request );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
218
		if ( method_exists( $e, 'render' ) && $response ) {
219
			return Router::toResponse( $request, $response );
220
		} elseif ( $e instanceof Responsable ) {
221
			return $e->toResponse( $request );
222
		}
223
224
		$e = $this->prepare_exception( $e );
225
226
		if ( $e instanceof HttpResponseException ) {
227
			return $e->getResponse();
228
		} elseif ( $e instanceof AuthenticationException ) {
229
			return $this->unauthenticated( $request, $e );
230
		} elseif ( $e instanceof ValidationException ) {
231
			return $this->convert_validation_exception_to_response( $e, $request );
232
		}
233
234
		return $request->expectsJson()
235
					? $this->prepare_json_response( $request, $e )
236
					: $this->prepare_response( $request, $e );
237
	}
238
239
	/**
240
	 * Prepare exception for rendering.
241
	 *
242
	 * @param \Throwable $e The throwable exception.
243
	 *
244
	 * @return \Throwable
245
	 */
246
	protected function prepare_exception( Throwable $e ) {
247
		if ( $e instanceof ModelNotFoundException ) {
248
			$e = new NotFoundHttpException( $e->getMessage(), $e );
249
		} elseif ( $e instanceof AuthorizationException ) {
250
			$e = new AccessDeniedHttpException( $e->getMessage(), $e );
251
		} elseif ( $e instanceof TokenMismatchException ) {
252
			$e = new HttpException( 419, $e->getMessage(), $e );
253
		} elseif ( $e instanceof SuspiciousOperationException ) {
254
			$e = new NotFoundHttpException( 'Bad hostname provided.', $e );
255
		}
256
257
		return $e;
258
	}
259
260
	/**
261
	 * Convert an authentication exception into a response.
262
	 *
263
	 * @param \Illuminate\Http\Request                 $request The app request.
264
	 * @param \Illuminate\Auth\AuthenticationException $exception The authenticated exception.
265
	 *
266
	 * @return \Symfony\Component\HttpFoundation\Response
267
	 */
268
	protected function unauthenticated( $request, AuthenticationException $exception ) {
269
		return $request->expectsJson()
270
					? response()->json( array( 'message' => $exception->getMessage() ), 401 )
0 ignored issues
show
Bug introduced by
The function response was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

270
					? /** @scrutinizer ignore-call */ response()->json( array( 'message' => $exception->getMessage() ), 401 )
Loading history...
271
					: redirect()->guest( $exception->redirectTo() ?? route( 'login' ) );
0 ignored issues
show
Bug introduced by
The function route was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

271
					: redirect()->guest( $exception->redirectTo() ?? /** @scrutinizer ignore-call */ route( 'login' ) );
Loading history...
Bug introduced by
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

271
					: /** @scrutinizer ignore-call */ redirect()->guest( $exception->redirectTo() ?? route( 'login' ) );
Loading history...
272
	}
273
274
	/**
275
	 * Create a response object from the given validation exception.
276
	 *
277
	 * @param \Illuminate\Validation\ValidationException $e The validation exception.
278
	 * @param \Illuminate\Http\Request                   $request The app request.
279
	 *
280
	 * @return \Symfony\Component\HttpFoundation\Response
281
	 */
282
	protected function convert_validation_exception_to_response( ValidationException $e, $request ) {
283
		if ( $e->response ) {
284
			return $e->response;
285
		}
286
287
		return $request->expectsJson()
288
					? $this->invalid_json( $request, $e )
289
					: $this->invalid( $request, $e );
290
	}
291
292
	/**
293
	 * Convert a validation exception into a response.
294
	 *
295
	 * @param \Illuminate\Http\Request                   $request The app request.
296
	 * @param \Illuminate\Validation\ValidationException $exception The validation exception.
297
	 *
298
	 * @return \Illuminate\Http\Response
299
	 */
300
	protected function invalid( $request, ValidationException $exception ) {
301
		return redirect( $exception->redirectTo ?? url()->previous() )
0 ignored issues
show
Bug introduced by
The function url was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

301
		return redirect( $exception->redirectTo ?? /** @scrutinizer ignore-call */ url()->previous() )
Loading history...
Bug introduced by
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

301
		return /** @scrutinizer ignore-call */ redirect( $exception->redirectTo ?? url()->previous() )
Loading history...
302
					->withInput( Arr::except( $request->input(), $this->dont_flash ) )
303
					->withErrors( $exception->errors(), $exception->errorBag );
304
	}
305
306
	/**
307
	 * Convert a validation exception into a JSON response.
308
	 *
309
	 * @param \Illuminate\Http\Request                   $request The app request.
310
	 * @param \Illuminate\Validation\ValidationException $exception The validation exception.
311
	 *
312
	 * @return \Illuminate\Http\JsonResponse
313
	 */
314
	protected function invalid_json( $request, ValidationException $exception ) {
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

314
	protected function invalid_json( /** @scrutinizer ignore-unused */ $request, ValidationException $exception ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
315
		return response()->json(
0 ignored issues
show
Bug introduced by
The function response was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

315
		return /** @scrutinizer ignore-call */ response()->json(
Loading history...
316
			array(
317
				'message' => $exception->getMessage(),
318
				'errors'  => $exception->errors(),
319
			),
320
			$exception->status
321
		);
322
	}
323
324
	/**
325
	 * Prepare a response for the given exception.
326
	 *
327
	 * @param \Illuminate\Http\Request $request The app request.
328
	 * @param \Throwable               $e The throwable exception.
329
	 *
330
	 * @return \Symfony\Component\HttpFoundation\Response
331
	 */
332
	protected function prepare_response( $request, Throwable $e ) {
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

332
	protected function prepare_response( /** @scrutinizer ignore-unused */ $request, Throwable $e ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
333
		if ( ! $this->is_http_exception( $e ) && $this->container['config']['app.debug'] ) {
334
			return $this->to_illuminate_response( $this->convert_exception_to_response( $e ), $e );
335
		}
336
337
		if ( ! $this->is_http_exception( $e ) ) {
338
			$e = new HttpException( 500, $e->getMessage() );
339
		}
340
341
		return $this->to_illuminate_response(
342
			$this->render_http_exception( $e ),
343
			$e
344
		);
345
	}
346
347
	/**
348
	 * Create a Symfony response for the given exception.
349
	 *
350
	 * @param \Throwable $e The throwable exception.
351
	 *
352
	 * @return \Symfony\Component\HttpFoundation\Response
353
	 */
354
	protected function convert_exception_to_response( Throwable $e ) {
355
		return SymfonyResponse::create(
0 ignored issues
show
Deprecated Code introduced by
The function Symfony\Component\HttpFo...tion\Response::create() has been deprecated: since Symfony 5.1, use __construct() instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

355
		return /** @scrutinizer ignore-deprecated */ SymfonyResponse::create(

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
356
			$this->render_exception_content( $e ),
357
			$this->is_http_exception( $e ) ? $e->getStatusCode() : 500,
0 ignored issues
show
Bug introduced by
The method getStatusCode() does not exist on Throwable. It seems like you code against a sub-type of Throwable such as Symfony\Component\HttpKe...\HttpExceptionInterface or Symfony\Component\HttpKe...Exception\HttpException. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

357
			$this->is_http_exception( $e ) ? $e->/** @scrutinizer ignore-call */ getStatusCode() : 500,
Loading history...
358
			$this->is_http_exception( $e ) ? $e->getHeaders() : array()
0 ignored issues
show
Bug introduced by
The method getHeaders() does not exist on Throwable. It seems like you code against a sub-type of Throwable such as Symfony\Component\HttpKe...\HttpExceptionInterface or Symfony\Component\HttpKe...Exception\HttpException. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

358
			$this->is_http_exception( $e ) ? $e->/** @scrutinizer ignore-call */ getHeaders() : array()
Loading history...
359
		);
360
	}
361
362
	/**
363
	 * Get the response content for the given exception.
364
	 *
365
	 * @param \Throwable $e The throwable exception.
366
	 *
367
	 * @return string
368
	 */
369
	protected function render_exception_content( Throwable $e ) {
370
		try {
371
			return $this->container['config']['app.debug'] && class_exists( Whoops::class )
372
						? $this->render_exception_with_whoops( $e )
373
						: $this->render_exception_with_symfony( $e, $this->container['config']['app.debug'] );
374
		} catch ( Exception $e ) {
375
			return $this->render_exception_with_symfony( $e, $this->container['config']['app.debug'] );
376
		}
377
	}
378
379
	/**
380
	 * Render an exception to a string using "Whoops".
381
	 *
382
	 * @param \Throwable $e The throwable exception.
383
	 *
384
	 * @return string
385
	 */
386
	protected function render_exception_with_whoops( Throwable $e ) {
387
		return tap(
388
			new Whoops(),
389
			function ( $whoops ) {
390
				$whoops->appendHandler( $this->whoops_handler() );
391
392
				$whoops->writeToOutput( false );
393
394
				$whoops->allowQuit( false );
395
			}
396
		)->handleException( $e );
397
	}
398
399
	/**
400
	 * Get the Whoops handler for the application.
401
	 *
402
	 * @return \Whoops\Handler\Handler
0 ignored issues
show
Bug introduced by
The type Whoops\Handler\Handler was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
403
	 */
404
	protected function whoops_handler() {
405
		try {
406
			return $this->container( HandlerInterface::class );
0 ignored issues
show
Bug introduced by
The method container() does not exist on WPB\Exceptions\Handler. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

406
			return $this->/** @scrutinizer ignore-call */ container( HandlerInterface::class );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
407
		} catch ( BindingResolutionException $e ) {
408
			return ( new WhoopsHandler() )->forDebug();
0 ignored issues
show
Bug introduced by
The type WPB\Exceptions\WhoopsHandler was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
409
		}
410
	}
411
412
	/**
413
	 * Render an exception to a string using Symfony.
414
	 *
415
	 * @param \Throwable $e The throwable exception.
416
	 * @param bool       $debug Enable or disable debug.
417
	 *
418
	 * @return string
419
	 */
420
	protected function render_exception_with_symfony( Throwable $e, $debug ) {
421
		$renderer = new HtmlErrorRenderer( $debug );
422
423
		return $renderer->getBody( $renderer->render( $e ) );
424
	}
425
426
	/**
427
	 * Render the given HttpException.
428
	 *
429
	 * @param \Symfony\Component\HttpKernel\Exception\HttpExceptionInterface $e The http exception.
430
	 *
431
	 * @return \Symfony\Component\HttpFoundation\Response
432
	 */
433
	protected function render_http_exception( HttpExceptionInterface $e ) {
434
		$this->register_error_view_paths();
435
		$view = $this->get_http_exception_view( $e );
436
437
		if ( view()->exists( $view ) ) {
0 ignored issues
show
Bug introduced by
The function view was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

437
		if ( /** @scrutinizer ignore-call */ view()->exists( $view ) ) {
Loading history...
438
			return response()->view(
0 ignored issues
show
Bug introduced by
The function response was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

438
			return /** @scrutinizer ignore-call */ response()->view(
Loading history...
439
				$view,
440
				array(
441
					'errors'    => new ViewErrorBag(),
442
					'exception' => $e,
443
				),
444
				$e->getStatusCode(),
445
				$e->getHeaders()
446
			);
447
		}
448
449
		return $this->convert_exception_to_response( $e );
450
	}
451
452
	/**
453
	 * Register the error template hint paths.
454
	 *
455
	 * @return void
456
	 */
457
	protected function register_error_view_paths() {
458
		$paths = collect( $this->container['config']['view.paths'] );
459
460
		View::replaceNamespace(
461
			'errors',
462
			$paths->map(
463
				function ( $path ) {
464
					return "{$path}/errors";
465
				}
466
			)->push( __DIR__ . '/views' )->all()
467
		);
468
	}
469
470
	/**
471
	 * Get the view used to render HTTP exceptions.
472
	 *
473
	 * @param \Symfony\Component\HttpKernel\Exception\HttpExceptionInterface $e The http exceptions.
474
	 *
475
	 * @return string
476
	 */
477
	protected function get_http_exception_view( HttpExceptionInterface $e ) {
478
		return "errors::{$e->getStatusCode()}";
479
	}
480
481
	/**
482
	 * Map the given exception into an Illuminate response.
483
	 *
484
	 * @param \Symfony\Component\HttpFoundation\Response $response The app http response.
485
	 * @param \Throwable                                 $e The throwable exception.
486
	 *
487
	 * @return \Illuminate\Http\Response
488
	 */
489
	protected function to_illuminate_response( $response, Throwable $e ) {
490
		if ( $response instanceof SymfonyRedirectResponse ) {
491
			$response = new RedirectResponse(
492
				$response->getTargetUrl(),
493
				$response->getStatusCode(),
494
				$response->headers->all()
495
			);
496
		} else {
497
			$response = new Response(
498
				$response->getContent(),
499
				$response->getStatusCode(),
500
				$response->headers->all()
501
			);
502
		}
503
504
		return $response->withException( $e );
0 ignored issues
show
Bug Best Practice introduced by
The expression return $response->withException($e) also could return the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
505
	}
506
507
	/**
508
	 * Prepare a JSON response for the given exception.
509
	 *
510
	 * @param \Illuminate\Http\Request $request The app http request.
511
	 * @param \Throwable               $e The throwable exception.
512
	 *
513
	 * @return \Illuminate\Http\JsonResponse
514
	 */
515
	protected function prepare_json_response( $request, Throwable $e ) {
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

515
	protected function prepare_json_response( /** @scrutinizer ignore-unused */ $request, Throwable $e ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
516
		return new Json_response(
0 ignored issues
show
Bug introduced by
The type WPB\Exceptions\Json_response was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
517
			$this->convert_exception_to_array( $e ),
518
			$this->is_http_exception( $e ) ? $e->getStatusCode() : 500,
519
			$this->is_http_exception( $e ) ? $e->getHeaders() : array(),
520
			JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES
521
		);
522
	}
523
524
	/**
525
	 * Convert the given exception to an array.
526
	 *
527
	 * @param \Throwable $e The throwable exception.
528
	 *
529
	 * @return array
530
	 */
531
	protected function convert_exception_to_array( Throwable $e ) {
532
		return $this->container['config']['app.debug'] ? array(
533
			'message'   => $e->getMessage(),
534
			'exception' => get_class( $e ),
535
			'file'      => $e->getFile(),
536
			'line'      => $e->getLine(),
537
			'trace'     => collect( $e->getTrace() )->map(
538
				function ( $trace ) {
539
					return Arr::except( $trace, array( 'args' ) );
540
				}
541
			)->all(),
542
		) : array(
543
			'message' => $this->is_http_exception( $e ) ? $e->getMessage() : 'Server Error',
544
		);
545
	}
546
547
	/**
548
	 * Render an exception to the console.
549
	 *
550
	 * @param \Symfony\Component\Console\Output\OutputInterface $output The symfony console output.
551
	 * @param \Throwable                                        $e The throwable exception.
552
	 *
553
	 * @return void
554
	 */
555
	public function render_for_console( $output, Throwable $e ) {
556
		( new ConsoleApplication() )->renderThrowable( $e, $output );
557
	}
558
559
	/**
560
	 * Determine if the given exception is an HTTP exception.
561
	 *
562
	 * @param \Throwable $e The throwable exception.
563
	 *
564
	 * @return bool
565
	 */
566
	protected function is_http_exception( Throwable $e ) {
567
		return $e instanceof HttpExceptionInterface;
568
	}
569
}
570