How to handle resource loading error? ( ResourceLoader.load() )

Is there a way to handle errors while loading resource just like with FileAccess?
Not only in case a file is not found but rather if “any” other error happens.

I found this in the docs but I have no idea how to use it:
Returns an empty resource if no ResourceFormatLoader could handle the file, and prints an error if no file is found at the specified path.

Working example with FileAccess:

var save_game = FileAccess.open("user://my_resource.tres", FileAccess.READ)
var error = FileAccess.get_open_error()
if error == ERR_FILE_NOT_FOUND:
	print("File not found, create a new file with default values.")
elif error != OK:
	printerr("An error has occured. Error code: " + str(error))

Incomplete + not working example with ResourceLoader:

var save_game: Resource = load("user://my_resource.tres")
print(save_game) # Prints <Object#null> if an error occurs
# How to handle loading error?

and prints an error if no file is found at the specified path

This means that it will print an error to the console, not return an Error type as FileAccess unfortunately.

I can see only this way to “handle the error”. It will not prevent the error from being printed to the console though.

var save_game: Resource = load("user://my_resource.tres")
if save_game == null:
	# handle error
else:
	# continue with your regular code
1 Like

This means that it will print an error to the console, not return an Error type as FileAccess unfortunately.

In that case then error handling is pretty limited compared to FileAccess and that’s not what I want.
So I wonder if I should open an issue about that on GitHub.

You could maybe combine both to check for the error with FileAccess and then load the resource with ResourceLoader

var save_game: Resource
FileAccess.open("user://my_resource.tres", FileAccess.READ)
var error = FileAccess.get_open_error()
if error == ERR_FILE_NOT_FOUND:
	print("File not found, create a new file with default values.")
elif error != OK:
	printerr("An error has occured. Error code: " + str(error))
else:
	save_game = load("user://my_resource.tres")

But you can of course open an issue on the GitHub too, as that would be a nice to have on the ResourceLoader directly.

Issue opened: Improve ResourceLoader error handling · Issue #11528 · godotengine/godot-proposals · GitHub

As for your solution see the section in this link: Is there a reason why this should be core and not an add-on in the asset library?

I don’t really know how to explain that but I think loading a file and reading it is probably different from reading a resource in terms of errors.

1 Like

Yes, of course. The solution I mentioned will get you basically only one error covered - in case the file is missing. It won’t handle any other errors that can be thrown by the ResourceLoader internally.

Since you know beforehand that this method could throw an error,
why not just ask if the file exists? e.g.:

if FileAccess.file_exists(path):
    var resource = ResourceLoader.load(path)
    # maybe an error if you mix up paths and the file turns out to 
    # "not" be a resource
else:
    # handle missing file

But it seems ResourceLoader has its own method specifically for Resources.

if ResourceLoader.exists(path):
    var resource = ResourceLoader.load(path)
else:
    # handle missing resource

So you are guaranteed to load a resource, if it exists.

And if you want to have more detailed errors you could combine the two. Let’s say you wrote your own custom resource with a specific file extension:

if ResourceLoader.exists(path):
    var resource = ResourceLoader.load(path)
elif FileAccess.file_exists(path):
    print('Only .xyz files are supported')
else:
    print('File does not exist')

You’re wrong in this sentence unfortunately. Loading a Resource with ResourceLoader can fail also for other reasons than just the path being incorrect. If a file at the provided path is not a proper Resource - it will not be loaded, even if the file exists.

1 Like

Quote from the doc → ResourceLoader.exists()

Returns whether a recognized resource exists for the given path.

If you wrote a custom resource, I guess it could fail, but, assuming you use the default Resources or you correctly wrote your own ResourceFormatSaver/ResourceFormatLoader, it should be impossible for the ResourcerLoader to fail.

1 Like

Fair point and actually might work.
@Arakeen you might want to try this then.

I didn’t understand why it’d work and how one would right correctly write his own ResourceFormaterSaver / Loader, how would it be done?

Also if it doesn’t work with a custom resource it’s still problematic in this case although if that solution works for the default ones it’d be nice.

Just do

var Ressouce = load(“res://icon.svg”)

If you want to acces it in all functions, do

@onready var a = load(“res://icon.svg”)

outside of any function

if it doesnt work, try preload()