【发布时间】:2019-11-09 19:17:53
【问题描述】:
我正在使用 Laravel 并尝试像使用 java 一样使用 try/catch。不幸的是,它没有按预期工作...该异常没有被捕获,并且没有返回错误消息,而是产生了 422 异常。
这是我的功能:
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();
}
}
我这样称呼这个方法
Route::post('memberform/changePassword','MemberController@changePassword')->name('changePassword');
在这里我想得到我的异常消息,并显示它。相反,我在使用我的请求时遇到了错误,并且没有捕获到这个异常
422 无法处理的实体 {"message":"给定的数据无效。","errors":{"newpassword":["新密码必须至少为 6 个字符。"]}}
非常感谢
【问题讨论】:
-
你是如何发送请求的?