Simplify resource opening error handling with goto

Use goto-style error handling in resource opening functions, which helps
prevent code duplication.

Also free resources on error in add_resource, fixing a potential memory
leak