I've noticed that in production mode with my debug mode set to false that most of my functions that have a try/catch will return the Laravel default "Server error" message.
I've been trying to hunt this message down with little luck, how can I customise this generic message returned from functions within my Laravel app whilst debug is turned off?
If you're referring to a very generic HTTP 500 error, it's a blade file in the framework.
If you want to display your own error for 5XX errors and such, you can override them by creating a blade file with the name of the error you want to override. For example:
resources/views/errors/500.blade.php
{{ __('Uh oh. Something has gone wrong behind the scenes.');
Now when a 500 error is encountered, your blade error will be displayed rather than the default Laravel 500 error.
You can create files for the common errors too obviously.
Update
The default messages for HTTPExceptions in Laravel are provided by the Symfony Response class found in the Symfony\Component\HttpFoundation directory.
When it comes to providing error messages in APIs, most will send the default HTTP status code in the headers and then supply a human error message in the response body.
For your example you might do something like:
return response()->json([
'errors' => [
[
'status' => 500,
'title' => 'Internal server error',
'message' => 'A more detailed error message to show the end user'
],
]
], 500);
You would then be responsible for consuming the error response and showing the end user the human readable error rather than the default generic Internal server error.
You might find some of the JSON API examples useful.
In my case, my Model extends Authenticable.
I changed it to Model and imported Eloquence.
Related
I don't get the correct validation error messages, instead I get this dot-notation error message:
When I submit the empty form, the fabric error message shows validation.required instead of Fabric is required.
Here is my code:
$request->validate([
'fabric'=>'required|in:3'
]);
I deleted the old resources/lang folder and it worked.
Just remember to have the new lang folder, and its content, in your project root directory like this : https://github.com/laravel/laravel/tree/9.x/lang
I use the default Laravel Exception handler, but I have a question regarding good practices.
When I have a condition in which the controller must throw an exception, I do the following:
if($blah) {
abort(500, "The user should not be here");
}
and then on the frontend, I have a view that handles the error as this:
#section('message', __($exception->getMessage() ?: 'Whoops, something went wrong on our servers.'))
So that works great as long as I have the following on the .env file:
APP_DEBUG=true
Whenever I change this value to false, (as it should in production), all I get on the error page is: Server Error instead of my custom message.
How can I keep my custom error message, and have APP_DEBUG=false in production?
If you want to customize you HTTP errors for specific views or conditions, then you will have to look into Rendering exceptions section that will show you how to show a custom view for a specific exception. With this you can throw an exception like
throw new UserNotFoundException();
and listen to that exception and render a specific view as required.
I am trying to access an api function, for example I want to access the login function, I get the following error:
"message": "An internal error occurred during your request!",
Looking in the log file, the following log is there:
ERROR 2017-10-09 17:01:37,296 [11 ]
nHandling.AbpApiExceptionFilterAttribute - Processing of the HTTP
request resulted in an exception. Please see the HTTP response
returned by the 'Response' property of this exception for details.
System.Web.Http.HttpResponseException: Processing of the HTTP request
resulted in an exception. Please see the HTTP response returned by the
'Response' property of this exception for details.
I am using Postman to test. My URL is https://localhost:44347/api/Account/Authenticate. My header has Context-Type as "application/json" and in my Body I have the loginModel formatted as
{
"tenancyName": "tenantname",
"userNameOrEmailAddress": "admin",
"password": "123qwe"
}
Am not sure how else to proceed on this. Any ideas please? I have swagger installed as well.
I am using MVC + AngularJS template. I have not changed anything, just the default project.
Appreciate the assistance.
I have tested with Fiddler and it works correctly.
Note: make sure you type Content-Type correctly, on the question you typed Context-Type
Make a POST request! You may be missing this.
So was trying to build in a notification when a new API token was created it would notify me. I added in the events and listeners it said too in the documentation but it is giving me a 500 error now when i try to create a new token. When I comment out the listener the token creation bit works - its only when I add the listener back in that it 500 errors again. The token itself is being stored in the DB but the "name" of the token is blank in the DB. However on the front end it just errors out - and when i look in my console i am getting a 500 error:
POST domain.com/oauth/personal-access-tokens 500 (Internal Server Error)
dispatchXhrRequest # app.js:11413
xhrAdapter # app.js:11250
dispatchRequest # app.js:11899
Event and listener:
'Laravel\Passport\Events\AccessTokenCreated' => [
'App\Listeners\RevokeOldTokens',
],
awww Thank you guys! was looking at the screen for too long!
when it auto generated the listener it put in:
use App\Events\Laravel\Passport\Events\AccessTokenCreated;
instead of
use Laravel\Passport\Events\AccessTokenCreated;
Hello I am trying to create my venues registration values from Laravel API (POST URL) and I am doing right when I check with postman and it shows me the success values and when I give the same routes controllers and whatever I need I give to the mobile app developer and when he tried to run the same thing which is obviously working fine it does not work there it shows
CLIENT PROTOCOL EXCEPTION I does not understand what to do
Any help will highly appreciated
org.apache.http.client.HttpResponseException: client protocol exception
If you used the guzzle http package, it is bundled with the CLientException error class which is an extension of the laravel RuntimeException. This is caused by a a wrong url fed into the guzzle http library.
for instance a
$http = new guzzleclient;
$response = $http->post('localhost/stack/overflow', [
'form_params' => [
"params" => "params"
],
]);
make sure you change the 'localhost/stack/overflow' url to your current server url, else it will throw errors.
I hope this helps.. Cheers