0

I'm missing something in my code and I would like to know where is the problem. I want to test if my API responses can handle exceptions but they only return 500 if the user is unauthorized.

I did look at this topic, in my case I would get a 401 error when I call this route.

Controller

   public function index()
    {
        try{
            return response()->json(Product::orderByDesc('updated_at')->get(), 200);
        } catch (\Exception $e) {
        throw new HttpException(401, $e->getMessage());
        }
    } 

What should I do ?

leri
  • 261
  • 1
  • 3
  • 12
  • Arguments are ordered incorrectly. Should be `HttpException($e->getMessage(), 401)`. – Tpojka Feb 08 '22 at 10:58
  • Yes but it still returns 500 – leri Feb 08 '22 at 13:07
  • check `storage/logs/laravel.log` there should be written where is error triggered. – Tpojka Feb 08 '22 at 16:24
  • To authorize data, what do you use? Are you using middleware, policy, or gate? And I think it would be better if you also tell us the Laravel version you are using. – Yusuf T. Feb 14 '22 at 06:00
  • I use laravel 8.83.0 and I have a middleware to check if the JWT is in the header of each request – leri Feb 23 '22 at 08:40

0 Answers0