Debugging class loader exceptions

Hey everyone,

So before I start…I don’t have this code posted anywhere public like github, I could probably upload a tarball or something if necessary, but I’d like to figure out how to diagnose this based on the error logging rather than creating another topic like the one I’ll mention below… :slight_smile:

I’ve been trying to create an app following the tutorial (https://docs.nextcloud.com/server/latest/developer_manual/app_development/tutorial.html) but when I’m trying to retrieve a particular item from one of my controllers I get a 500 response and in the logs it shows the message “Could not resolve service! Class service does not exist”. From what I’ve found so far, it seems that this likely just means I’ve got a typo somewhere in one of my class files that’s preventing it from constructing properly, but I haven’t been able to find exactly where that issue is.

I did find one forum post from last April that seems to be a similar issue:

The problem is that the solution posted involves an error in a class file that doesn’t seem to be mentioned anywhere in the stack trace from the posted error log, and there’s no information given with the solution about how that particular file was identified to be the culprit. So assuming there’s a typo in one of my class files, is there some way I can test/validate those to at least determine exactly which file has the issue, or better yet identify the specific line number? How do I find the actual error based on this error message?

The full error I’m seeing in my logs is:

{"reqId":"1zdCeJL12BApI4uiumye","level":3,"time":"2021-09-21T19:35:53+00:00","remoteAddr":"192.168.1.193","user":"admin","app":"index","method":"GET","url":"/index.php/apps/scsensors/cells","message":{"Exception":"OCP\\AppFramework\\QueryException","Message":"Could not resolve service! Class service does not exist","Code":0,"Trace":[{"file":"/var/www/nextcloud/lib/private/AppFramework/Utility/SimpleContainer.php","line":126,"function":"resolve","class":"OC\\AppFramework\\Utility\\SimpleContainer","type":"->","args":["service"]},{"file":"/var/www/nextcloud/lib/private/ServerContainer.php","line":156,"function":"query","class":"OC\\AppFramework\\Utility\\SimpleContainer","type":"->","args":["service",true]},{"file":"/var/www/nextcloud/lib/private/AppFramework/DependencyInjection/DIContainer.php","line":430,"function":"query","class":"OC\\ServerContainer","type":"->","args":["service",true]},{"file":"/var/www/nextcloud/lib/private/AppFramework/Utility/SimpleContainer.php","line":96,"function":"query","class":"OC\\AppFramework\\DependencyInjection\\DIContainer","type":"->","args":["service"]},{"function":"OC\\AppFramework\\Utility\\{closure}","class":"OC\\AppFramework\\Utility\\SimpleContainer","type":"->","args":["*** sensitive parameters replaced ***"]},{"file":"/var/www/nextcloud/lib/private/AppFramework/Utility/SimpleContainer.php","line":101,"function":"array_map","args":[{"__class__":"Closure"},[{"name":"AppName","__class__":"ReflectionParameter"},{"name":"request","__class__":"ReflectionParameter"},"*** sensitive parameter replaced ***",{"name":"UserId","__class__":"ReflectionParameter"}]]},{"file":"/var/www/nextcloud/lib/private/AppFramework/Utility/SimpleContainer.php","line":109,"function":"buildClass","class":"OC\\AppFramework\\Utility\\SimpleContainer","type":"->","args":[{"name":"OCA\\SCSensors\\Controller\\CellController","__class__":"ReflectionClass"}]},{"file":"/var/www/nextcloud/lib/private/AppFramework/Utility/SimpleContainer.php","line":126,"function":"resolve","class":"OC\\AppFramework\\Utility\\SimpleContainer","type":"->","args":["OCA\\SCSensors\\Controller\\CellController"]},{"file":"/var/www/nextcloud/lib/private/AppFramework/DependencyInjection/DIContainer.php","line":455,"function":"query","class":"OC\\AppFramework\\Utility\\SimpleContainer","type":"->","args":["OCA\\SCSensors\\Controller\\CellController"]},{"file":"/var/www/nextcloud/lib/private/AppFramework/DependencyInjection/DIContainer.php","line":427,"function":"queryNoFallback","class":"OC\\AppFramework\\DependencyInjection\\DIContainer","type":"->","args":["OCA\\SCSensors\\Controller\\CellController"]},{"file":"/var/www/nextcloud/lib/private/AppFramework/App.php","line":139,"function":"query","class":"OC\\AppFramework\\DependencyInjection\\DIContainer","type":"->","args":["OCA\\SCSensors\\Controller\\CellController"]},{"file":"/var/www/nextcloud/lib/private/Route/Router.php","line":309,"function":"main","class":"OC\\AppFramework\\App","type":"::","args":["OCA\\SCSensors\\Controller\\CellController","listTypes",{"__class__":"OC\\AppFramework\\DependencyInjection\\DIContainer"},{"_route":"scsensors.cell.listTypes"}]},{"file":"/var/www/nextcloud/lib/base.php","line":1008,"function":"match","class":"OC\\Route\\Router","type":"->","args":["/apps/scsensors/cells"]},{"file":"/var/www/nextcloud/index.php","line":37,"function":"handleRequest","class":"OC","type":"::","args":[]}],"File":"/var/www/nextcloud/lib/private/AppFramework/Utility/SimpleContainer.php","Line":115,"CustomMessage":"--"},"userAgent":"Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Goanna/4.8 Firefox/68.0 PaleMoon/29.1.1","version":"20.0.11.1"}

Can you share a link to your app code?

Managed to get the issue resolved.

My advice to anyone in this situation would be to ignore the stack track in the error message and don’t bother messing with your unit tests…just start by completely commenting all references to the service class out of the controller and see if the error persists. If not then restore the service and proceed to comment the mapper and object out of the service and just keep going down the stack like that…and once you find which class is causing it, comment each individual function of that class in a binary search pattern to identify which function has the error…and then if needed do the same thing to comment the individual lines of that function. It’s annoying, it takes a while, but it’ll work.

My other piece of advice…is make sure you edit using a font that doesn’t make parens and brackets look too similar :slight_smile: