-2

I have experienced a 500 server error via Laravel, and no error is being written to the log file. I've verified permissions are OK, and that Laravel can write to the log file. I am unsure what other options I have here.

I have chased the error down to this class, the commented out lines are the lines that are causing the error.

class CollectionResource extends JsonResource
{
    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
            // 'member_count' => $this->items()->count(),
            // 'member_count_today' => $this->items()->whereDate('collection_entries.created_at', Carbon::today()->toDateString())->count(),
            // 'member_images' => $this->items->take(10)->pluck('data.picture'),
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
        ];
    }
}

Even when wrapping the code in a trycatch, it still swallows the error and returns a 500 without any response data.

Debugging is enabled, logging is enabled, basic things have been checked and double checked. I don't see how I can get passed this?

I purposely threw a syntax exception, it threw a parse error so I know the trycatch block works, and the code is reaching where it should be.

Upon the second request of this endpoint, it seems to work fine with no changes. It is only this specific endpoint.

Items is a belongsToMany relationship.

Sam
  • 43
  • 7
  • Have you checked the Apache (or whatever web server you are using) error logs? – Brian Nov 05 '20 at 10:37
  • @Brian no logs at all, no laravel logs, no php logs, no web server logs, I've checked the webserver access logs to verify its hitting the webserver and it is. I even overrided Laravel's exception handler to purposely return the exception and it swallowed it. – Sam Nov 05 '20 at 10:39
  • Put your app in debug-mode: set `APP_DEBUG=true` in `.env`, refresh the cache by running `php artisan cache:clear`, and you'll see the error directly on your screen. – Qirel Nov 05 '20 at 10:40
  • @Qirel yep tried that, debug is already set to true and I've tried clearing all caches. – Sam Nov 05 '20 at 10:41
  • A wild guess is that you haven't imported `Carbon`, but if you're not looking at the correct errors (because Laravel will show them very clearly if you enable app-debug mode), all we can do is guess wildly. Could also be that some of your methods are returning NULL. But again, get the actual error message. – Qirel Nov 05 '20 at 10:43
  • @Qirel that wouldn't explain why the endpoint works after its initial hit. Both of these have been checked and verified to not be the causing issue. It's a strange one. – Sam Nov 05 '20 at 10:44

1 Answers1

-1

The 500 Internal Server Error in particular is a catch-all error message, given when no more specific message is suitable. There can be a number of causes for a 500 Internal Server Error to display in a web browser.

  1. Check the error logs
  2. Check the .htaccess file
  3. Check your PHP resources
  4. Check CGI/Perl scripts
  5. Check for any possible wrong typo in your PHP
Burhan Kashour
  • 674
  • 5
  • 15
  • 100% of your "checklist" has already been covered. – Sam Nov 05 '20 at 10:39
  • How did you enable the error debugging in your php?? – Burhan Kashour Nov 05 '20 at 10:40
  • Through php.ini – Sam Nov 05 '20 at 10:41
  • Did you restart your apache server after enabling it? – Burhan Kashour Nov 05 '20 at 10:41
  • You don't need to restart apache or any web server for that matter, I restarted my PHP yes. I fail to see why you think web server has anything to do with PHP logs? Web server logs were already enabled and have been checked. I would render your current answer useless at this stage. – Sam Nov 05 '20 at 10:42
  • Some PHP exceptions are not possible to catch, so Laravel cant handle them. For example syntax errors, or maximum memory limit errors. And yes, you need to restart most of the times to get everything works perfectly you should restart your apache server. – Burhan Kashour Nov 05 '20 at 10:48
  • The web server is a totally different process to PHP, you're just spreading bad advice at this stage. PHP can handle any exception you want it to, it just takes additional configuration which I have done. If you read my question you will see I am handling the error with PHP and not Laravel by using an explicit trycatch around the code in my post. – Sam Nov 05 '20 at 10:50