The HTTP status code "0" is not valid. 报错
环境
本机环境: win 7
homestead:lc-homestead-8.2.1-2019112300()
Laravel version:7.13.0
问题描述
《L05 Laravel 教程 - 电商实战 》(8.6. 用户界面 - 申请退款) 我想测试将错误提示给用户。就将 if (!$order->paid_at)
改为 if ($order->paid_at)
,使程序进入到错误提示。
// 判断订单是否已付款
if ($order->paid_at) {
throw new InvalidRequestException('该订单未支付,不可退款');
}
我提交退款申请提示 The HTTP status code "0" is not valid.
结果截图
代码
app/Http/Controllers/OrdersController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests\OrderRequest;
use App\Models\ProductSku;
use App\Models\UserAddress;
use App\Models\Order;
use Carbon\Carbon;
use App\Services\OrderService;
use App\Exceptions\InvalidRequestException;
use App\Http\Requests\SendReviewRequest;
use App\Events\OrderReviewed;
use App\Http\Requests\ApplyRefundRequest;
class OrdersController extends Controller
{
.
.
.
public function applyRefund(Order $order, ApplyRefundRequest $request)
{
// 校验订单是否属于当前用户
$this->authorize('own', $order);
// 判断订单是否已付款
if ($order->paid_at) {
throw new InvalidRequestException('该订单未支付,不可退款');
}
// 判断订单退款状态是否正确
// if (!($order->refund_status !== Order::REFUND_STATUS_PENDING)) {
// throw new InvalidRequestException('该订单已经申请过退款,请勿重复申请');
// }
// // 将用户输入的退款理由放入订单的 extra 字段中
// $extra = $order->extra ?:[];
// $extra['refund_reason'] = $request->input('reason');
// // 将订单退款状态改为已申请退款
// $order->update([
// 'refund_status' => Order::REFUND_STATUS_APPLIED,
// 'extra' => $extra,
// ]);
return $order;
}
}
resources/views/orders/show.blade.php
.
.
.
// 退款按钮点击事件
$('#btn-apply-refund').click(function () {
swal({
text: '请输入退款理由',
content: 'input',
}).then(function (input) {
// 当用户点击 swal 弹出框上的按钮时触发这个函数
if (!input) {
swal('退款理由不能为空', '', 'error');
return;
}
// 请求退款接口
axios.post('{{ route('orders.apply_refund', [$order->id]) }}', {reason: input})
.then(function(){
swal('申请退款成功', '', 'success').then(function () {
// 用户点击弹窗上按钮时重新加载页面
location.reload();
});
},function(error) {
if (error.response && error.response.status === 401) {
swal('请先登录', '', 'error');
} else if (error.response && (error.response.data.msg || error.response.data.message)) {
// 其他有 msg 或者 message 字段的情况,将 msg 提示给用户
swal(error.response.data.msg ? error.response.data.msg : error.response.data.message, '', 'error');
} else {
// 其他情况应该是系统挂了
swal('系统错误', '', 'error');
}
});
});
});
App/Exceptions/InvalidRequestException.php
<?php
namespace App\Exceptions;
use Exception;
use Illuminate\Http\Request;
class InvalidRequestException extends Exception
{
public function __contruct(string $message = "", int $code = 400)
{
parent::__contruct($message, $code);
}
public function render(Request $request)
{
if ($request->expectsJson()) {
// json() 方法第二个参数就是 Http 返回码
return response()->json(['msg' => $this->message], $this->code);
}
return view('pages.error', ['msg' => $this->message]);
}
}
尝试
我觉得问题出在App/Exceptions/InvalidRequestException.php
。修改InvalidRequestException.php
代码,看程序跑到哪一步。修改如下:
namespace App\Exceptions;
use Exception;
use Illuminate\Http\Request;
class InvalidRequestException extends Exception
{
public function __contruct(string $message = "", int $code = 400)
{
parent::__contruct($message, $code);
}
public function render(Request $request)
{
// 修改部分
if ($request->expectsJson()) {
// json() 方法第二个参数就是 Http 返回码
return response()->json(['msg' => '11111'], 400);
// return response()->json(['msg' => $this->message], $this->code);
}
return response()->json(['msg' => '22222'], 400);
// return view('pages.error', ['msg' => $this->message]);
// 修改部分END
}
}
结果是通过了if ($request->expectsJson())
判断。
接着修改InvalidRequestException.php
代码,查看code的值
if ($request->expectsJson()) {
// json() 方法第二个参数就是 Http 返回码
return response()->json(['msg' => "$this->code"], 400);
// return response()->json(['msg' => $this->message], $this->code);
}
结果输出0
根据上面测试,我了解到了code
为 0
。这里想不明白code
怎么会变成了0
,初始化应该是400
才对的。
class InvalidRequestException extends Exception
{
public function __contruct(string $message = "", int $code = 400)
{
parent::__contruct($message, $code);
}
.
.
.
调用的时候也没有传0
过去。
// 判断订单是否已付款
if ($order->paid_at) {
throw new InvalidRequestException('该订单未支付不可退款');
}
我设下code
为666
// 判断订单是否已付款
if ($order->paid_at) {
throw new InvalidRequestException('该订单未支付不可退款', 666);
}
结果显示666
走到这里。了解到了code
为0
导致报错The HTTP status code "0" is not valid.
。至于code
怎么变为0
,我不知道怎么查了。求高手指条路
推荐文章: