Skip to content

Commit 177ca3f

Browse files
committed
Add error handler in Dingo API
1 parent a6eb94b commit 177ca3f

File tree

6 files changed

+61
-52
lines changed

6 files changed

+61
-52
lines changed

app/Exceptions/Handler.php

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -45,43 +45,9 @@ public function report(Exception $e)
4545
*/
4646
public function render($request, Exception $e)
4747
{
48-
echo 'abcdefg123';
49-
5048
if ($e instanceof ModelNotFoundException) {
5149
$e = new NotFoundHttpException($e->getMessage(), $e);
52-
} elseif ($e instanceof Tymon\JWTAuth\Exceptions\TokenExpiredException) {
53-
$message = 'Token expired';
54-
$error = new Error('token_expired');
55-
$statusCode = $e->getStatusCode();
56-
57-
return response()->apiJsonError($message, $error, $statusCode);
58-
} elseif ($e instanceof Tymon\JWTAuth\Exceptions\TokenInvalidException) {
59-
$message = 'Token invalid';
60-
$error = new Error('token_invalid');
61-
$statusCode = $e->getStatusCode();
62-
63-
return response()->apiJsonError($message, $error, $statusCode);
64-
} elseif ($e instanceof ExceedingIndexException) {
65-
echo 'qqq123';
66-
67-
return response()->apiJsonError(
68-
$e->getMessage(),
69-
$e->getErrors(),
70-
$e->statusCode());
71-
} elseif ($e instanceof AbstractException) {
72-
echo 'qqq456';
73-
74-
return response()->apiJsonError(
75-
$e->getMessage(),
76-
$e->getErrors(),
77-
$e->statusCode());
78-
} elseif ($e instanceof Exception) {
79-
echo 'qqq000';
80-
81-
return response()->apiJsonError(
82-
'jim',
83-
'000',
84-
$e->statusCode()); }
50+
}
8551

8652
return parent::render($request, $e);
8753
}

app/Http/Controllers/Api/V1_0/VolunteerProfileController.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,7 @@ public function updateSkillsMe(UpdateSkillsRequest $request)
6969

7070
if (ArrayUtil::isIndexExceed($skillsList, $maxIndex)) {
7171
// Index exceeds $skillsList size
72-
$message = 'Unable to execute';
73-
$error = new Error('exceeding_index_value');
74-
$statusCode = 400;
75-
76-
return response()->apiJsonError($message, $error, $statusCode);
72+
throw new ExceedingIndexException();
7773
}
7874
}
7975

@@ -100,11 +96,7 @@ public function updateEquipmentMe(UpdateEquipmentRequest $request)
10096

10197
if (ArrayUtil::isIndexExceed($equipmentList, $maxIndex)) {
10298
// Index exceeds $equipmentList size
103-
$message = 'Unable to execute';
104-
$error = new Error('exceeding_index_value');
105-
$statusCode = 400;
106-
107-
return response()->apiJsonError($message, $error, $statusCode);
99+
throw new ExceedingIndexException();
108100
}
109101
}
110102

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace App\Providers;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
use App\Exceptions\AbstractException;
7+
use App\Http\Responses\Responses;
8+
use Tymon\JWTAuth\Exceptions\TokenExpiredException;
9+
use Tymon\JWTAuth\Exceptions\TokenInvalidException;
10+
11+
class ApiErrorHandlerServiceProvider extends ServiceProvider
12+
{
13+
/**
14+
* Bootstrap the application services.
15+
* Register custom error response in Dingo API.
16+
* Reference: https://github.com/dingo/api/wiki/Errors-And-Error-Responses#custom-exception-responses
17+
*
18+
* @return void
19+
*/
20+
public function boot()
21+
{
22+
app('Dingo\Api\Exception\Handler')->register(function (AbstractException $e) {
23+
return response()->apiJsonError(
24+
$e->getMessage(),
25+
$e->getErrors(),
26+
$e->getStatusCode());
27+
});
28+
29+
app('Dingo\Api\Exception\Handler')->register(function (TokenExpiredException $e) {
30+
$message = 'Token expired';
31+
$error = new Error('token_expired');
32+
$statusCode = $e->getStatusCode();
33+
34+
return response()->apiJsonError($message, $error, $statusCode);
35+
});
36+
37+
app('Dingo\Api\Exception\Handler')->register(function (TokenInvalidException $e) {
38+
$message = 'Token invalid';
39+
$error = new Error('token_invalid');
40+
$statusCode = $e->getStatusCode();
41+
42+
return response()->apiJsonError($message, $error, $statusCode);
43+
});
44+
45+
}
46+
47+
/**
48+
* Register the application services.
49+
*
50+
* @return void
51+
*/
52+
public function register()
53+
{
54+
//
55+
}
56+
}

app/Providers/ErrorResponseMacroServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function boot(ResponseFactory $factory)
3232
'errors' => [ $errors ]
3333
];
3434

35-
return $factory->json($jsonResponse, $statusCode);
35+
return response()->make($jsonResponse, $statusCode);
3636
});
3737
}
3838

config/app.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@
148148
App\Providers\ErrorResponseMacroServiceProvider::class,
149149
Tymon\JWTAuth\Providers\JWTAuthServiceProvider::class,
150150
Barryvdh\Cors\ServiceProvider::class,
151+
App\Providers\ApiErrorHandlerServiceProvider::class,
151152
],
152153

153154
/*

tests/VolunteerProfileControllerTest.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,6 @@ public function testSuccessfullyUpdateSkillsMe()
7575
'Authorization' => 'Bearer ' . $token,
7676
'X-VMS-API-Key' => $this->apiKey
7777
])
78-
//->seeJson([
79-
// 'username' => 'a'
80-
//])
8178
->assertResponseStatus(204);
8279

8380
$testVolunter = App\Volunteer::find($volunteer->id);
@@ -155,9 +152,6 @@ public function testSuccessfullyUpdateEqiupmentMe()
155152
'Authorization' => 'Bearer ' . $token,
156153
'X-VMS-API-Key' => $this->apiKey
157154
])
158-
//->seeJson([
159-
// 'username' => 'a'
160-
//])
161155
->assertResponseStatus(204);
162156

163157
$testVolunter = App\Volunteer::find($volunteer->id);

0 commit comments

Comments
 (0)