I am working with Laravel and try to use try/catch as I would do for java. Unfortunately, it doesn't work as expected... The exception isn't catched, and instead of returning an error message, it makes a 422 exception.
Here is my function:
public function changePassword(Request $request){
try{
if (!(Hash::check($request->get('currentpassword'), Auth::user()->password))) {
return "Your current password does not matches with the password you provided. Please try again.";
}
if(strcmp($request->get('currentpassword'), $request->get('new-password')) == 0){
return "New Password cannot be same as your current password. Please choose a different password.";
}
$validatedData = $request->validate([
'currentpassword' => 'required',
'newpassword' => 'required|string|min:6',
]);
$user = Auth::user();
$user->password = bcrypt($request->get('newpassword'));
$user->save();
return "Password changed successfully !";
}
catch(Exception $error){
return $error->getMessage();
}
}
I call this method like this
Route::post('memberform/changePassword','MemberController@changePassword')->name('changePassword');
Here i would like to get my exception message, and display it. Instead, I get an error while using my request, and this exception isn't catched
422 Unprocessable Entity {"message":"The given data was invalid.","errors":{"newpassword":["The newpassword must be at least 6 characters."]}}
Thanks a lot