1

I have a request to log all validation exceptions in my application. I have all of it working except for the request parameters are empty. I'm able to dd the request parameters, and it displays, but it doesn't seem to log. How is the $data value getting wiped?

In app\Exceptions\Handler.php:

public function report(Throwable $exception)
{
    if ($exception instanceof ValidationException) {
        $data = json_encode(collect(request()->all())->map(function ($value, $key) {
            return (in_array($key, $this->dontFlash)) ? '(redacted)' : $value;
        })->toArray());

        Log::info('[VALIDATION ERROR][' . request()->ip() . '][DATA] ' . $data . '[ERRORS] ' .
            json_encode($exception->errors()));
    }

    parent::report($exception);
}

Example log (see that $data is now null?):

[VALIDATION ERROR][127.0.0.1][DATA] [ERRORS] {"email":["The email must be a valid email address."],"password":["The password must be at least 10 characters and contain at least one uppercase character, one number, and one special character."]}

Expected:

[VALIDATION ERROR][127.0.0.1][DATA] {"_token":"JY5euwiygtZCCp02l9Jo1usHBywEruWywGnYy8ft","first_name":"f","last_name":"f","email":"f@fa","password":"(redacted)","password_confirmation":"(redacted)"} [ERRORS] {"email":["The email must be a valid email address."],"password":["The password must be at least 10 characters and contain at least one uppercase character, one number, and one special character."]}

--

$data = json_encode(collect(request()->all())->map(function ($value, $key) {
    return (in_array($key, $this->dontFlash)) ? '(redacted)' : $value;
})->toArray());

dd($data);

returns: {"_token":"JY5euwiygtZCCp02l9Jo1usHBywEruWywGnYy8ft","first_name":"f","last_name":"f","email":"f@fa","password":"(redacted)","password_confirmation":"(redacted)"}

What is causing this? How am I able to see the request parameters in the request? I've tried using reflection and extracting out the data from the Validator object, and that was empty too.

Karl Hill
  • 12,937
  • 5
  • 58
  • 95
ahinkle
  • 2,117
  • 3
  • 29
  • 58
  • Terminable middleware might help you solve this problem. https://laravel.com/docs/8.x/middleware#terminable-middleware – usrNotFound Dec 10 '20 at 23:08
  • This may be the pbm: https://stackoverflow.com/questions/19361282/why-would-json-encode-return-an-empty-string – 4givN Dec 11 '20 at 03:58

0 Answers0