I added proengsoft/laravel-jsvalidation to my laravel 6 app which works ok on my
ubuntu 18 with PHP 7.2.24
But My client pulling the version got error on his Windows / php 7.4.2 as :
#57 C:\\xampp\\htdocs\\primewatch\\public\\index.php(55): Illuminate\\Foundation\\Http\\Kernel->handle(Object(Illuminate\\Http\\Request))
#58 {main}
[previous exception] [object] (Symfony\\Component\\Debug\\Exception\\FatalThrowableError(code: 0): Argument 2 passed to Proengsoft\\JsValidation\\JsValidatorFactory::__construct() must be of the type array, null given, called in C:\\xampp\\htdocs\\primewatch\\vendor\\proengsoft\\laravel-jsvalidation\\src\\JsValidationServiceProvider.php on line 38 at C:\\xampp\\htdocs\\primewatch\\vendor\\proengsoft\\laravel-jsvalidation\\src\\JsValidatorFactory.php:37)
[stacktrace]
#0 C:\\xampp\\htdocs\\primewatch\\vendor\\proengsoft\\laravel-jsvalidation\\src\\JsValidationServiceProvider.php(38): Proengsoft\\JsValidation\\JsValidatorFactory->__construct(Object(Illuminate\\Foundation\\Application), NULL)
#1 C:\\xampp\\htdocs\\primewatch\\vendor\\laravel\\framework\\src\\Illuminate\\Container\\Container.php(799): Proengsoft\\JsValidation\\JsValidationServiceProvider->Proengsoft\\JsValidation\\{closure}(Object(Illuminate\\Foundation\\Application), Array)
#2 C:\\xampp\\htdocs\\primewatch\\vendor\\laravel\\framework\\src\\Illuminate\\Container\\Container.php(681): Illuminate\\Container\\Container->build(Object(Closure))
#3 C:\\xampp\\htdocs\\primewatch\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Application.php(785): Illuminate\\Container\\Container->resolve('jsvalidator', Array, true)
#4 C:\\xampp\\htdocs\\primewatch\\vendor\\laravel\\framework\\src\\Illuminate\\Container\\Container.php(629): Illuminate\\Foundation\\Application->resolve('jsvalidator', Array)
#5 C:\\xampp\\htdocs\\primewatch\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Application.php(770): Illuminate\\Container\\Container->make('jsvalidator', Array)
#6 C:\\xampp\\htdocs\\primewatch\\vendor\\laravel\\framework\\src\\Illuminate\\Container\\Container.php(1245): Illuminate\\Foundation\\Application->make('jsvalidator')
#7 C:\\xampp\\htdocs\\primewatch\\vendor\\laravel\\framework\\src\\Illuminate\\Support\\Facades\\Facade.php(198): Illuminate\\Container\\Container->offsetGet('jsvalidator')
#8 C:\\xampp\\htdocs\\primewatch\\vendor\\laravel\\framework\\src\\Illuminate\\Support\\Facades\\Facade.php(166): Illuminate\\Support\\Facades\\Facade::resolveFacadeInstance('jsvalidator')
#9 C:\\xampp\\htdocs\\primewatch\\vendor\\laravel\\framework\\src\\Illuminate\\Support\\Facades\\Facade.php(255): Illuminate\\Support\\Facades\\Facade::getFacadeRoot()
#10 C:\\xampp\\htdocs\\primewatch\\storage\\framework\\views\\68757a9c9b2f658eb9358cc98549740ca7f9e23d.php(34): Illuminate\\Support\\Facades\\Facade::__callStatic('formRequest', Array)
#11 C:\\xampp\\htdocs\\primewatch\\vendor\\laravel\\framework\\src\\Illuminate\\View\\Engines\\PhpEngine.php(43): include('C:\\\\xampp\\\\htdocs...')
#12 C:\\xampp\\htdocs\\primewatch\\vendor\\laravel\\framework\\src\\Illuminate\\View\\Engines\\CompilerEngine.php(59): Illuminate\\View\\Engines\\PhpEngine->evaluatePath('C:\\\\xampp\\\\htdocs...', Array)
#13 C:\\xampp\\htdocs\\primewatch\\vendor\\facade\\ignition\\src\\Views\\Engines\\CompilerEngine.php(36): Illuminate\\View\\Engines\\CompilerEngine->get('C:\\\\xampp\\\\htdocs...', Array)
#14 C:\\xampp\\htdocs\\primewatch\\vendor\\laravel\\framework\\src\\Illuminate\\View\\View.php(143): Facade\\Ignition\\Views\\Engines\\CompilerEngine->get('C:\\\\xampp\\\\htdocs...', Array)
I have app/Http/Requests/ShipRequest.php with :
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Request;
use App\Http\Traits\funcsTrait;
use App\Ship;
class ShipRequest extends FormRequest
{
use funcsTrait;
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
$request= Request();
return Ship::getValidationRulesArray( $request->get('id') );
}
}
And in Ship Model method getValidationRulesArray is defined as :
public static function getValidationRulesArray($ship_id= null) : array
{
$validationRulesArray = [
'title' => [
'required',
'string',
'max:255',
Rule::unique(with(new Ship)->getTable())->ignore($ship_id),
],
'company_id' => 'required|exists:' . ( with(new Company)->getTable() ).',id',
];
return $validationRulesArray;
}
I wonder why such error on Windows/php 7.4. Can it be php different issue or what ?
"laravel/framework": "^6.2",
"proengsoft/laravel-jsvalidation": "^2.5",
Thanks!
make sure use JsValidatorFacade not JsValidatorFactory
you can use this syntac
use Proengsoft\JsValidation\Facades\JsValidatorFacade as JsValidator;
Sometimes, all you need is to run following command
php artisan config:cache
because validator is not loading the jsvalidation config file just created in config directory
Hope it helps somebody.
In Laravel 5.8 app after submitting form with the data I need to clear data, like doubled spaces, so I created middleware
with clearing code :
app/Http/Middleware/WorkTextString.php :
<?php
namespace App\Http\Middleware;
use Closure;
use App\Http\Traits\funcsTrait;
use function PHPSTORM_META\type;
class WorkTextString
{
use funcsTrait;
public function handle($request, Closure $next, $strip_tags_excluding= false )
{
$inputDataArray = $request->all();
\Log::info($request->all());
$stripTagsExcludingArray= $this->pregSplit('/ /',$strip_tags_excluding);
foreach( $inputDataArray as $next_field_name=>$next_field_value ) {
if ( !empty($next_field_value) and is_string($next_field_value) ) {
$skip_strip_tags= in_array($next_field_name,$stripTagsExcludingArray);
$inputDataArray[$next_field_name] = $this->workTextString($next_field_value, $skip_strip_tags);
}
}
\Log::info('$inputDataArray:: ::'); // I CHECK AND SEE CLEARED DATA!
\Log::info($inputDataArray);
$request->replace($inputDataArray); // THAT DOWS NOT WORK ?
return $next($request);
}
}
But I see that the submitted data are not cleared. Looks like $request->replace does not work for me.
routes/api.php :
Route::resource('skills', 'API\Admin\SkillController')->middleware('WorkTextString');
Modified :
I found article https://dev.to/samolabams/transforming-laravel-request-data-using-middleware-2k7j
and reading the article I suppose I have to use cleanData of my middleware, so I commented method I used before and added
method :
private function cleanData(array $data)
{
\Log::info('$data:: ::');
\Log::info($data);
return collect($data)->map(function ($value, $key) {
\Log::info('$value:: ::');
\Log::info($value);
$value= 'Some text ';
return $value;
})->all();
}
Just to check and replace any value
BuT i got error in log file:
[2019-12-24 12:40:48] local.ERROR: Function name must be a string {"userId":1,"exception":"[object] (Symfony\\Component\\Debug\\Exception\\FatalThrowableError(code: 0): Function name must be a string at /mnt/_work_sdb8/wwwroot/lar/CTasksRestAPI/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:164)
[stacktrace]
#0 /mnt/_work_sdb8/wwwroot/lar/CTasksRestAPI/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#1 /mnt/_work_sdb8/wwwroot/lar/CTasksRestAPI/vendor/tymon/jwt-auth/src/Http/Middleware/Authenticate.php(32): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#2 /mnt/_work_sdb8/wwwroot/lar/CTasksRestAPI/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(163): Tymon\\JWTAuth\\Http\\Middleware\\Authenticate->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#3 /mnt/_work_sdb8/wwwroot/lar/CTasksRestAPI/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#4 /mnt/_work_sdb8/wwwroot/lar/CTasksRestAPI/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php(41): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#5 /mnt/_work_sdb8/wwwroot/lar/CTasksRestAPI/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(163): Illuminate\\Routing\\Middleware\\SubstituteBindings->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#6 /mnt/_work_sdb8/wwwroot/lar/CTasksRestAPI/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#7 /mnt/_work_sdb8/wwwroot/lar/CTasksRestAPI/vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php(58): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
...
and no message I wrote in \Log::info...
Which way is right ?
How to do it correctly ?
I found a decision using method in model for any fields needs clearing:
public function setNameAttribute($value)
{
$this->attributes['name'] = $this->workTextString($value);
}
without middleware and it works for me
I'm trying to use lumen passport for our project. I already installed by following this lumen-passport. I successfully receive the access_token by doing postman request and here is my code for it.
{
"grant_type": "client_credentials",
"client_id": "2",
"client_secret": "1QaxEr6P3K6kKsqa63nA2SMLsczuJJRvufXEDrzY",
"scope": "*"
}
I receive the access token
{
"token_type": "Bearer",
"expires_in": 120,
"access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6ImJmY2YyYjZlM2I4MTlkMzI3OTE5ODgyMjM0M2NlNjcxM2MyYjFkMjJjZDFiNTQ5ODQ5MjkxMzllMDc2NWJmM2UyZGMyMTMxYTRmYzk1MzNkIn0.eyJhdWQiOiIyIiwianRpIjoiYmZjZjJiNmUzYjgxOWQzMjc5MTk4ODIyMzQzY2U2NzEzYzJiMWQyMmNkMWI1NDk4NDkyOTEzOWUwNzY1YmYzZTJkYzIxMzFhNGZjOTUzM2QiLCJpYXQiOjE1Njg2MTg4ODksIm5iZiI6MTU2ODYxODg4OSwiZXhwIjoxNTY4NjE5MDA5LCJzdWIiOiIiLCJzY29wZXMiOlsiKiJdfQ.hIXxn1MJMSS6m2XXrt-EJatMp7KoomzKYnk_McpeeBo9VMYUtKU7tPWCmtw7XpFZFJWvGhIY8cx_A6kTaizFjqEkKmlj3jpjs9X9QUZNQ4J5CwjIcXUAJLRqw6WYvvW94GdPAgUBFz6eIbg_Tzt-149dIwPzlVpd8Ln3Bu84Htj9tiWalkdu5EL6lO_Mc8mFJpAh63fJs84_ES02ex_MACsO52pwfXQLPdrjyWSHPdqDj0hRPVMVMLtPdNtYS12MVf8xg_C6KdcB19viRhmlnQwjl98AXGYt_YeeJozKQax2bSSTqTHfNrBZpka7FZFMznkS3gQ8-9d9FYNSNu3Hiia2ZN44JkolBXB4bNpaa82cj_2yrQ-w8oFivOQqX-dQU7RDD5womVbCd8VmxmtUoTTXdWjridl0F5XasWJfc1N2vTGleY_AGi1qC5_39QXTXsEeesgsiGvq4OJCeIRSLvuuwpFUbV7LVfUvJVL3HSM9-PUBDw0Q2q7mCVLMf_ZLf5s_rNP7uTZOIpZUvUKYXCk4rCB6dE7YIGQZkBr2sOQADd8foMvnxEatwIiJoQCxAEHRgeybmBk4gDUhr-9IoroHxnXrcg2LPulfwe5-hS8cAkjh7OvUuld8W17_TzWOUJq1XPpgWCRomSAB_KeXdyFzrGyVXJTD2r_DU1HbJGE"}
in my routes I have these codes.
$router->group(['prefix' => 'api/v1', 'middleware' => 'client'], function() use (&$router){
$router->get('example/{id}', 'ExampleController#getData');});
Why is it every time the token expires I get 500 error instead 401 error?
I checked the logs in the storage logs and here is the error. I don't know if this is really an error.
[2019-09-17 02:39:59] local.ERROR: Illuminate\Auth\AuthenticationException: Unauthenticated. in /var/www/scheduler-app/vendor/laravel/passport/src/Http/Middleware/CheckClientCredentials.php:48
Stack trace:
#0 /var/www/scheduler-app/vendor/illuminate/pipeline/Pipeline.php(163): Laravel\Passport\Http\Middleware\CheckClientCredentials->handle(Object(Laravel\Lumen\Http\Request), Object(Closure))
#1 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Laravel\Lumen\Http\Request))
#2 /var/www/scheduler-app/vendor/laravel/lumen-framework/src/Routing/Pipeline.php(32): call_user_func(Object(Closure), Object(Laravel\Lumen\Http\Request))
#3 /var/www/scheduler-app/vendor/illuminate/pipeline/Pipeline.php(104): Laravel\Lumen\Routing\Pipeline->Laravel\Lumen\Routing\{closure}(Object(Laravel\Lumen\Http\Request))
#4 /var/www/scheduler-app/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(413): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#5 /var/www/scheduler-app/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(259): Laravel\Lumen\Application->sendThroughPipeline(Array, Object(Closure))
#6 /var/www/scheduler-app/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(233): Laravel\Lumen\Application->handleFoundRoute(Array)
#7 /var/www/scheduler-app/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(169): Laravel\Lumen\Application->handleDispatcherResponse(Array)
#8 /var/www/scheduler-app/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(416): Laravel\Lumen\Application->Laravel\Lumen\Concerns\{closure}(Object(Laravel\Lumen\Http\Request))
#9 /var/www/scheduler-app/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(171): Laravel\Lumen\Application->sendThroughPipeline(Array, Object(Closure))
#10 /var/www/scheduler-app/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(108): Laravel\Lumen\Application->dispatch(NULL)
#11 /var/www/scheduler-app/public/index.php(28): Laravel\Lumen\Application->run()
#12 {main} {"exception":"[object] (Illuminate\\Auth\\AuthenticationException(code: 0): Unauthenticated. at /var/www/scheduler-app/vendor/laravel/passport/src/Http/Middleware/CheckClientCredentials.php:48)
[stacktrace]
#0 /var/www/scheduler-app/vendor/illuminate/pipeline/Pipeline.php(163): Laravel\\Passport\\Http\\Middleware\\CheckClientCredentials->handle(Object(Laravel\\Lumen\\Http\\Request), Object(Closure))
#1 [internal function]: Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Laravel\\Lumen\\Http\\Request))
#2 /var/www/scheduler-app/vendor/laravel/lumen-framework/src/Routing/Pipeline.php(32): call_user_func(Object(Closure), Object(Laravel\\Lumen\\Http\\Request))
#3 /var/www/scheduler-app/vendor/illuminate/pipeline/Pipeline.php(104): Laravel\\Lumen\\Routing\\Pipeline->Laravel\\Lumen\\Routing\\{closure}(Object(Laravel\\Lumen\\Http\\Request))
#4 /var/www/scheduler-app/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(413): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))
#5 /var/www/scheduler-app/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(259): Laravel\\Lumen\\Application->sendThroughPipeline(Array, Object(Closure))
#6 /var/www/scheduler-app/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(233): Laravel\\Lumen\\Application->handleFoundRoute(Array)
#7 /var/www/scheduler-app/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(169): Laravel\\Lumen\\Application->handleDispatcherResponse(Array)
#8 /var/www/scheduler-app/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(416): Laravel\\Lumen\\Application->Laravel\\Lumen\\Concerns\\{closure}(Object(Laravel\\Lumen\\Http\\Request))
#9 /var/www/scheduler-app/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(171): Laravel\\Lumen\\Application->sendThroughPipeline(Array, Object(Closure))
#10 /var/www/scheduler-app/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(108): Laravel\\Lumen\\Application->dispatch(NULL)
#11 /var/www/scheduler-app/public/index.php(28): Laravel\\Lumen\\Application->run()
#12 {main}
I know this is not the best answer for those developers who know lumen passport, but I figure it out the other way to fix it. I just copy the code from CheckClientCredentials, as what I have checked, the code validate the request. here it is
public function handle($request, Closure $next, ...$scopes)
{
$psr = (new DiactorosFactory)->createRequest($request);
try {
$psr = $this->server->validateAuthenticatedRequest($psr);
} catch (OAuthServerException $e) {
throw new AuthenticationException;
}
$this->validateScopes($psr, $scopes);
return $next($request);
}
In my Authentication middleware I add this code.
public function handle($request, Closure $next, $guard = null)
{
$psr = (new DiactorosFactory)->createRequest($request);
try {
$psr = $this->server->validateAuthenticatedRequest($psr);
} catch (OAuthServerException $e) {
return response('Unauthorized.', 401);
}
// if ($this->auth->guard($guard)->guest()) {
// return response('Unauthorized.', 401);
// }
return $next($request);
}
and the routes I have this
$router->group(['prefix' => 'api/v1', 'middleware' => ['auth', 'client']], function() use (&$router){
$router->get('example/{id}', 'ExampleController#getData');
});
It is working now. I receive 401 error and it validates token if it is not expire.
Instead of changing the middleware, just add something like this to the render method of App\Exceptions\Handler
if ($exception instanceof AuthenticationException && $request->acceptsJson()) {
return response()->json(['error' => $exception->getMessage()], 401);
}
Thanks for your solution. I have added a check if it is really a 401 error.
public function handle($request, Closure $next, ...$scopes)
{
$psr = (new PsrHttpFactory(
new Psr17Factory,
new Psr17Factory,
new Psr17Factory,
new Psr17Factory
))->createRequest($request);
try {
$psr = $this->server->validateAuthenticatedRequest($psr);
} catch (OAuthServerException $e) {
if($e->getHttpStatusCode() === 401) {
return response('Unauthorized.', 401);
}
throw new AuthenticationException;
}
$this->validate($psr, $scopes);
return $next($request);
}
Anyone know what is the best way to handle errors in Laravel, there is any rules or something to follow ?
Currently i'm doing this :
public function store(Request $request)
{
$plate = Plate::create($request->all());
if ($plate) {
return $this->response($this->plateTransformer->transform($plate));
} else {
// Error handling ?
// Error 400 bad request
$this->setStatusCode(400);
return $this->responseWithError("Store failed.");
}
}
And the setStatusCode and responseWithError come from the father of my controller :
public function setStatusCode($statusCode)
{
$this->statusCode = $statusCode;
return $this;
}
public function responseWithError ($message )
{
return $this->response([
'error' => [
'message' => $message,
'status_code' => $this->getStatusCode()
]
]);
}
But is this a good way to handle the API errors, i see some different way to handle errors on the web, what is the best ?
Thanks.
Try this, i have used it in my project (app/Exceptions/Handler.php)
public function render($request, Exception $exception)
{
if ($request->wantsJson()) { //add Accept: application/json in request
return $this->handleApiException($request, $exception);
} else {
$retval = parent::render($request, $exception);
}
return $retval;
}
Now Handle Api exception
private function handleApiException($request, Exception $exception)
{
$exception = $this->prepareException($exception);
if ($exception instanceof \Illuminate\Http\Exception\HttpResponseException) {
$exception = $exception->getResponse();
}
if ($exception instanceof \Illuminate\Auth\AuthenticationException) {
$exception = $this->unauthenticated($request, $exception);
}
if ($exception instanceof \Illuminate\Validation\ValidationException) {
$exception = $this->convertValidationExceptionToResponse($exception, $request);
}
return $this->customApiResponse($exception);
}
After that custom Api handler response
private function customApiResponse($exception)
{
if (method_exists($exception, 'getStatusCode')) {
$statusCode = $exception->getStatusCode();
} else {
$statusCode = 500;
}
$response = [];
switch ($statusCode) {
case 401:
$response['message'] = 'Unauthorized';
break;
case 403:
$response['message'] = 'Forbidden';
break;
case 404:
$response['message'] = 'Not Found';
break;
case 405:
$response['message'] = 'Method Not Allowed';
break;
case 422:
$response['message'] = $exception->original['message'];
$response['errors'] = $exception->original['errors'];
break;
default:
$response['message'] = ($statusCode == 500) ? 'Whoops, looks like something went wrong' : $exception->getMessage();
break;
}
if (config('app.debug')) {
$response['trace'] = $exception->getTrace();
$response['code'] = $exception->getCode();
}
$response['status'] = $statusCode;
return response()->json($response, $statusCode);
}
Always add Accept: application/json in your api or json request.
Laravel is already able to manage json responses by default.
Withouth customizing the render method in app\Handler.php you can simply throw a Symfony\Component\HttpKernel\Exception\HttpException, the default handler will recognize if the request header contains Accept: application/json and will print a json error message accordingly.
If debug mode is enabled it will output the stacktrace in json format too.
Here is a quick example:
<?php
...
use Symfony\Component\HttpKernel\Exception\HttpException;
class ApiController
{
public function myAction(Request $request)
{
try {
// My code...
} catch (\Exception $e) {
throw new HttpException(500, $e->getMessage());
}
return $myObject;
}
}
Here is laravel response with debug off
{
"message": "My custom error"
}
And here is the response with debug on
{
"message": "My custom error",
"exception": "Symfony\\Component\\HttpKernel\\Exception\\HttpException",
"file": "D:\\www\\myproject\\app\\Http\\Controllers\\ApiController.php",
"line": 24,
"trace": [
{
"file": "D:\\www\\myproject\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\ControllerDispatcher.php",
"line": 48,
"function": "myAction",
"class": "App\\Http\\Controllers\\ApiController",
"type": "->"
},
{
"file": "D:\\www\\myproject\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Route.php",
"line": 212,
"function": "dispatch",
"class": "Illuminate\\Routing\\ControllerDispatcher",
"type": "->"
},
...
]
}
Using HttpException the call will return the http status code of your choice (in this case internal server error 500)
In my opinion I'd keep it simple.
Return a response with the HTTP error code and a custom message.
return response()->json(['error' => 'You need to add a card first'], 500);
Or if you want to throw a caught error you could do :
try {
// some code
} catch (Exception $e) {
return response()->json(['error' => $e->getMessage()], 500);
}
You can even use this for sending successful responses:
return response()->json(['activeSubscription' => $this->getActiveSubscription()], 200);
This way no matter which service consumes your API it can expect to receive the same responses for the same requests.
You can also see how flexible you can make it by passing in the HTTP status code.
If you are using Laravel 8+, you can do it simply by adding these lines in Exception/Handler.php on register() method
$this->renderable(function (NotFoundHttpException $e, $request) {
if ($request->is('api/*')) {
return response()->json([
'message' => 'Record not found.'
], 404);
}
});
For me, the best way is to use specific Exception for API response.
If you use Laravel version > 5.5, you can create your own exception with report() and render() methods. Use command:
php artisan make:exception AjaxResponseException
It will create AjaxResponseException.php at: app/Exceptions/
After that fill it with your logic. For example:
/**
* Report the exception.
*
* #return void
*/
public function report()
{
\Debugbar::log($this->message);
}
/**
* Render the exception into an HTTP response.
*
* #param \Illuminate\Http\Request $request
* #return JsonResponse|Response
*/
public function render($request)
{
return response()->json(['error' => $this->message], $this->code);
}
Now, you can use it in your ...Controller with try/catch functionality.
For example in your way:
public function store(Request $request)
{
try{
$plate = Plate::create($request->all());
if ($plate) {
return $this->response($this->plateTransformer->transform($plate));
}
throw new AjaxResponseException("Plate wasn't created!", 404);
}catch (AjaxResponseException $e) {
throw new AjaxResponseException($e->getMessage(), $e->getCode());
}
}
That's enough to make your code more easier for reading, pretty and useful.
Best regards!
For Laravel 8+ in file App\Exceptions\Hander.php inside method register() paste this code:
$this->renderable(function (Throwable $e, $request) {
if ($request->is('api/*')) {
return response()->json([
'message' => $e->getMessage(),
'code' => $e->getCode(),
], 404);
}
});
I think it would be better to modify existing behaviour implemented in app/Exceptions/Handler.php than overriding it.
You can modify JSONResponse returned by parent::render($request, $exception); and add/remove data.
Example implementation:
app/Exceptions/Handler.php
use Illuminate\Support\Arr;
// ... existing code
public function render($request, Exception $exception)
{
if ($request->is('api/*')) {
$jsonResponse = parent::render($request, $exception);
return $this->processApiException($jsonResponse);
}
return parent::render($request, $exception);
}
protected function processApiException($originalResponse)
{
if($originalResponse instanceof JsonResponse){
$data = $originalResponse->getData(true);
$data['status'] = $originalResponse->getStatusCode();
$data['errors'] = [Arr::get($data, 'exception', 'Something went wrong!')];
$data['message'] = Arr::get($data, 'message', '');
$originalResponse->setData($data);
}
return $originalResponse;
}
Well, all answers are ok right now, but also they are using old ways.
After Laravel 8, you can simply change your response in register() method by introducing your exception class as renderable:
<?php
namespace Your\Namespace;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
class Handler extends ExceptionHandler
{
/**
* Register the exception handling callbacks for the application.
*
* #return void
*/
public function register()
{
$this->renderable(function (NotFoundHttpException $e, $request) {
if ($request->is('api/*')) {
return response()->json([
'message' => 'Record not found.'
], 404);
}
});
}
}
Using some code from #RKJ best answer I have handled the errors in this way:
Open "Illuminate\Foundation\Exceptions\Handler" class and search for a method named "convertExceptionToArray". This method converts the HTTP exception into an array to be shown as a response. In this method, I have just tweaked a small piece of code that will not affect loose coupling.
So replace convertExceptionToArray method with this one
protected function convertExceptionToArray(Exception $e, $response=false)
{
return config('app.debug') ? [
'message' => $e->getMessage(),
'exception' => get_class($e),
'file' => $e->getFile(),
'line' => $e->getLine(),
'trace' => collect($e->getTrace())->map(function ($trace) {
return Arr::except($trace, ['args']);
})->all(),
] : [
'message' => $this->isHttpException($e) ? ($response ? $response['message']: $e->getMessage()) : 'Server Error',
];
}
Now navigate to the App\Exceptions\Handler class and paste the below code just above the render method:
public function convertExceptionToArray(Exception $e, $response=false){
if(!config('app.debug')){
$statusCode=$e->getStatusCode();
switch ($statusCode) {
case 401:
$response['message'] = 'Unauthorized';
break;
case 403:
$response['message'] = 'Forbidden';
break;
case 404:
$response['message'] = 'Resource Not Found';
break;
case 405:
$response['message'] = 'Method Not Allowed';
break;
case 422:
$response['message'] = 'Request unable to be processed';
break;
default:
$response['message'] = ($statusCode == 500) ? 'Whoops, looks like something went wrong' : $e->getMessage();
break;
}
}
return parent::convertExceptionToArray($e,$response);
}
Basically, we overrided convertExceptionToArray method, prepared the response message, and called the parent method by passing the response as an argument.
Note: This solution will not work for Authentication/Validation errors but most of the time these both errors are well managed by Laravel with proper human-readable response messages.
In your handler.php This should work for handling 404 Exception.
public function render($request, Throwable $exception ){
if ($exception instanceof ModelNotFoundException) {
return response()->json([
'error' => 'Data not found'
], 404);
}
return parent::render($request, $exception);
}
You don't have to do anything special. Illuminate\Foundation\Exceptions\Handler handles everything for you. When you pass Accept: Application/json header it will return json error response. if debug mode is on you will get exception class, line number, file, trace if debug is off you will get the error message. You can override convertExceptionToArray. Look at the default implementation.
return config('app.debug') ? [
'message' => $e->getMessage(),
'exception' => get_class($e),
'file' => $e->getFile(),
'line' => $e->getLine(),
'trace' => collect($e->getTrace())->map(function ($trace) {
return Arr::except($trace, ['args']);
})->all(),
] : [
'message' => $this->isHttpException($e) ? $e->getMessage() : 'Server Error',
];
As #shahib-khan said,
this happens in debug mode and is handled by Laravel in production mode.
you can see base method code in
\Illuminate\Foundation\Exceptions\Handler::convertExceptionToArray
protected function convertExceptionToArray(Throwable $e)
{
return config('app.debug') ? [
'message' => $e->getMessage(),
'exception' => get_class($e),
'file' => $e->getFile(),
'line' => $e->getLine(),
'trace' => collect($e->getTrace())->map(fn ($trace) => Arr::except($trace, ['args']))->all(),
] : [
'message' => $this->isHttpException($e) ? $e->getMessage() : 'Server Error',
];
}
Therefore, I overrode the the convertExceptionToArray function in app/Exceptions/Handler
of course, still in debug mode, if the exception is thrown, you can track it in the Laravel.log.
protected function convertExceptionToArray(Throwable $e)
{
return [
'message' => $this->isHttpException($e) ? $e->getMessage() : 'Server Error',
];
}
Add header to your API endpoint. which works for me. it will handle the error request properly.
Accept: application/json
I created a block with a toolbar, but an error happened:
Fatal error: Call to a member function setCurPage() on a non-object
I did quite some search-queries but can’t find the solution.
Is there someone who knows the reason?
Please see my code below:
class test_Promotion_Block_List extends Mage_Catalog_Block_Product_List {
public function __construct() {
parent::__construct();
$collection = Mage::getModel('catalog/product')
->getCollection()
->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left')
->addAttributeToSelect('*')
->addAttributeToFilter('category_id', array('finset' => '98'))
->addAttributeToSort('created_At', 'desc')
;
$this->setCollection($collection);
}
protected function _prepareLayout() {
parent::_prepareLayout();
$toolbar = $this->getToolbarBlock();
// called prepare sortable parameters
$collection = $this->getCollection();
// use sortable parameters
if ($orders = $this->getAvailableOrders()) {
$toolbar->setAvailableOrders($orders);
}
if ($sort = $this->getSortBy()) {
$toolbar->setDefaultOrder($sort);
}
if ($dir = $this->getDefaultDirection()) {
$toolbar->setDefaultDirection($dir);
}
$toolbar->setCollection($collection);
$this->setChild('toolbar', $toolbar);
$this->getCollection()->load();
return $this;
}
public function getDefaultDirection() {
return 'asc';
}
public function getAvailableOrders() {
return array('name' => 'Name', 'position' => 'Position', 'children_count' => 'Sub Category Count');
}
public function getSortBy() {
return 'name';
}
public function getToolbarBlock() {
$block = $this->getLayout()->createBlock('testpromotion/toolbar', microtime());
return $block;
}
public function getMode() {
return $this->getChild('toolbar')->getCurrentMode();
}
public function getToolbarHtml() {
return $this->getChildHtml('toolbar');
}
}
Error sniffing:
Magento Product_List blocks are "pagination aware". They take URL paging parameters and apply it to the collection of products to be displayed.
That means that the error you're seeing occurs somewhere in the parent classes of your block. That method is called for collections so it means that the result of one of the selects is not an object but either an array or null.
It's more likely you're receiving an array response but you didn't initialize the collection with the response so calling the method on an array triggers this error.
Error info:
Please specify the full error info including file and line where it occurs. This will help find the source of the error.
Also use the following line next to (before / after any operation that might change the $collection variable) because you may be calling $this->setCollection(null).
var_dump(is_object($collection) ? get_class($collection) : get_type($collection));