Skip to content

Commit 7149646

Browse files
author
Maxim Lanin
committed
Add namespace to views
1 parent bca8d10 commit 7149646

File tree

3 files changed

+69
-4
lines changed

3 files changed

+69
-4
lines changed

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,16 @@ The goal of this package is to provide you with a set of most common exceptions
1616

1717
To get the latest version of Laravel Laravel-API-Debugger, simply add the following line to the require block of your `composer.json` file.
1818

19+
For Laravel 5.1
1920
```
2021
"lanin/laravel-api-exceptions": "^0.1.0"
2122
```
2223

24+
For Laravel 5.3
25+
```
26+
"lanin/laravel-api-exceptions": "^0.3.0"
27+
```
28+
2329
You'll then need to run `composer install` or `composer update` to download it and have the autoloader updated.
2430

2531
Once Laravel-API-Exceptions is installed, you need to register the service provider. Open up `config/app.php` and add the following to the providers key.
@@ -30,7 +36,7 @@ Lanin\Laravel\ApiExceptions\ApiExceptionsServiceProvider::class,
3036

3137
### Exceptions
3238

33-
Every ApiException can be thrown as a normal exception and they will be automatically serialized to JSON with corresponding HTTP status:
39+
Every ApiException can be thrown as a normal exception and they will be automatically serialized to JSON with corresponding HTTP status, if user wants json:
3440

3541
```json
3642
{
@@ -64,6 +70,16 @@ Also it can have `meta` attribute when there is additional info. For example for
6470
For `ValidationApiException`, meta attribute has `errors` object that contains validations errors.
6571
Every attribute of this object is a name of a request parameter to validate to and value is an array of errors with description.
6672

73+
### Views
74+
75+
Since version 0.3.0 for Laravel 5.3 package can also return html view of the error, if `Accept` header not equals `application/json`.
76+
77+
To change included views publish them via:
78+
79+
```
80+
$ php artisan vendor:publish --tag=laravel-api-exceptions
81+
```
82+
6783
### Handler
6884

6985
Extend your default exceptions handler with:
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Method Not Allowed.</title>
5+
6+
<link href="https://fonts.googleapis.com/css?family=Lato:100" rel="stylesheet" type="text/css">
7+
8+
<style>
9+
html, body {
10+
height: 100%;
11+
}
12+
13+
body {
14+
margin: 0;
15+
padding: 0;
16+
width: 100%;
17+
color: #B0BEC5;
18+
display: table;
19+
font-weight: 100;
20+
font-family: 'Lato', sans-serif;
21+
}
22+
23+
.container {
24+
text-align: center;
25+
display: table-cell;
26+
vertical-align: middle;
27+
}
28+
29+
.content {
30+
text-align: center;
31+
display: inline-block;
32+
}
33+
34+
.title {
35+
font-size: 72px;
36+
margin-bottom: 40px;
37+
}
38+
</style>
39+
</head>
40+
<body>
41+
<div class="container">
42+
<div class="content">
43+
<div class="title">{{ $exception->getMessage() }}</div>
44+
</div>
45+
</div>
46+
</body>
47+
</html>

src/ExceptionHandlerTrait.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function render($request, Exception $e)
3636
{
3737
$e = $this->resolveException($e);
3838

39-
$response = $request->expectsJson() || env('DEBUG')
39+
$response = $request->expectsJson() || !function_exists('view')
4040
? $this->renderForApi($e)
4141
: $this->renderHtmlPage($e);
4242

@@ -65,8 +65,10 @@ protected function renderHtmlPage(ApiException $e)
6565
$status = $e->getCode();
6666

6767
return view()->exists("errors.{$status}")
68-
? response()->view("errors.{$status}", ['exception' => $e], $status, $e->getHeaders())
69-
: $this->renderForApi($e);
68+
? response(view("errors.{$status}", ['exception' => $e]), $status, $e->getHeaders())
69+
: (view()->exists("laravel-api-exceptions::errors.{$status}")
70+
? response(view("laravel-api-exceptions::errors.{$status}", ['exception' => $e]), $status, $e->getHeaders())
71+
: $this->renderForApi($e));
7072
}
7173

7274
/**

0 commit comments

Comments
 (0)