Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
L
laravel-adminpanel
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
laravel-adminpanel
Commits
eb0fbfa6
Commit
eb0fbfa6
authored
Mar 11, 2018
by
Viral Solani
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
- Add common error handeling function
- Refactoring
parent
5210f03f
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
59 additions
and
25 deletions
+59
-25
Handler.php
app/Exceptions/Handler.php
+59
-25
No files found.
app/Exceptions/Handler.php
View file @
eb0fbfa6
...
...
@@ -74,49 +74,29 @@ class Handler extends ExceptionHandler
if
(
$exception
instanceof
MethodNotAllowedHttpException
)
{
return
response
()
->
json
((
object
)
[
'status'
=>
false
,
'errorCode'
=>
'METHOD_NOT_ALLOWED'
,
'message'
=>
'Please check HTTP Request Method. - MethodNotAllowedHttpException'
],
403
);
return
$this
->
setStatusCode
(
403
)
->
respondWithError
(
'Please check HTTP Request Method. - MethodNotAllowedHttpException'
);
}
if
(
$exception
instanceof
NotFoundHttpException
)
{
return
response
()
->
json
((
object
)
[
'status'
=>
false
,
'errorCode'
=>
'URL_NOT_FOUND'
,
'message'
=>
'Please check your URL to make sure request is formatted properly. - NotFoundHttpException'
],
403
);
return
$this
->
setStatusCode
(
403
)
->
respondWithError
(
'Please check your URL to make sure request is formatted properly. - NotFoundHttpException'
);
}
if
(
$exception
instanceof
GeneralException
)
{
return
response
()
->
json
((
object
)
[
'status'
=>
false
,
'errorCode'
=>
'EXCEPTION'
,
'message'
=>
$exception
->
getMessage
()
],
403
);
return
$this
->
setStatusCode
(
403
)
->
respondWithError
(
$exception
->
getMessage
());
}
if
(
$exception
instanceof
ModelNotFoundException
)
{
return
response
()
->
json
((
object
)
[
'status'
=>
false
,
'errorCode'
=>
'ITEM_NOT_FOUND'
,
'message'
=>
'Item could not be found. Please check identifier.'
],
403
);
return
$this
->
setStatusCode
(
403
)
->
respondWithError
(
'Item could not be found. Please check identifier.'
);
}
if
(
$exception
instanceof
ValidationException
)
{
\Log
::
debug
(
"API Validation Exception - "
.
json_encode
(
$exception
->
validator
->
messages
()));
return
response
()
->
json
((
object
)
[
'status'
=>
false
,
'errorCode'
=>
'VALIDATION_EXCEPTION'
,
'messages'
=>
$exception
->
validator
->
messages
()
],
403
);
return
$this
->
setStatusCode
(
422
)
->
respondWithError
(
$exception
->
validator
->
messages
());
}
}
...
...
@@ -139,4 +119,58 @@ class Handler extends ExceptionHandler
return
redirect
()
->
guest
(
route
(
'frontend.auth.login'
));
}
/**
* get the status code.
*
* @return statuscode
*/
public
function
getStatusCode
()
{
return
$this
->
statusCode
;
}
/**
* set the status code.
*
* @param [type] $statusCode [description]
*
* @return statuscode
*/
public
function
setStatusCode
(
$statusCode
)
{
$this
->
statusCode
=
$statusCode
;
return
$this
;
}
/**
* respond with error.
*
* @param $message
*
* @return \Illuminate\Http\JsonResponse
*/
protected
function
respondWithError
(
$message
)
{
return
$this
->
respond
([
'error'
=>
[
'message'
=>
$message
,
'status_code'
=>
$this
->
getStatusCode
(),
],
]);
}
/**
* Respond.
*
* @param array $data
* @param array $headers
*
* @return \Illuminate\Http\JsonResponse
*/
public
function
respond
(
$data
,
$headers
=
[])
{
return
response
()
->
json
(
$data
,
$this
->
getStatusCode
(),
$headers
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment