Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel - Prevent errors from being shown in console

I'm doing an ajax request and if the validation fails, the user is presented with an error message.

However, the user can also see the error message in console. For example, if the user doesn't complete one of the fields, in console, this is what it shows:

Failed to load resource: the server responded with a status of 422 (Unprocessable Entity)

I do not want them to see the error. How can I prevent them from seeing errors like such and also other error in console, like 501 errors and stuff. I just simply want to display an alert saying "An error occurred". I don't want to show anything in console.

My current AJAX code:

(function ($) {
        $('#settingsForm').submit(function (e) {
            $.ajax({
                type: "POST",
                dataType: 'JSON',
                url: '/settings',
                data: $('#settingsForm').serialize(),
                beforeSend: function () {

                },
                success: function (data) {
                    message(data);
                },
                error: function (data) {

                    if (data.status === 422 ) {

                        var errors = $.parseJSON(data.responseText);
                        errorsHtml = '<div class="alert alert-danger"><ul>';
                        $.each(errors, function (index, value) {
                            errorsHtml += '<li>' + value + '</li>';
                        });
                        errorsHtml += '</ul></div>'
                        $('#status_settings').html(errorsHtml);
                    }

                }
            })
            ;

            e.preventDefault();
        })
        ;
    })
    (window.jQuery);
like image 347
Taylor Avatar asked Dec 07 '15 17:12

Taylor


People also ask

How do I turn off error reporting in Laravel?

You may turn off error details by setting the debug option in your app/config/app. php file to false .

What is Exception Handler in Laravel?

When you start a new Laravel project, error and exception handling is already configured for you. The App\Exceptions\Handler class is where all exceptions thrown by your application are logged and then rendered to the user.

How do you do error reporting in Laravel?

Through your config/app. php , set 'debug' => env('APP_DEBUG', false), to true . Or in a better way, check out your . env file and make sure to set the debug element to true.


1 Answers

It looks like you are using Laravel Form Request validation which sends a 422 status code for failed validation along with errors in case if AJAX form submission. There are two things you can do now to change the status code and remove the error message ( only in this case ) from the console:

  1. Override the response function in vendor/laravel/framework/src/Illuminate/Foundation/Http/FormRequest.php and change the status code to 200 in your new function. Copy the function from there and paste it in Request class in app/Http/Requests/Request.php. Change the following line

    return new JsonResponse($errors, 422);
    

    to something like this:

    $errors['status'] = 'error'; return new JsonResponse($errors, 200);

  2. Instead of using Form Request Validation, create a Validator instance for validation in your controller functions and return JSON response upon failed validation with 200 status code.

like image 85
Vikas Avatar answered Sep 30 '22 23:09

Vikas