Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Complete lack of error handling #17

Open
Epicguru opened this issue Jan 2, 2025 · 0 comments
Open

Complete lack of error handling #17

Epicguru opened this issue Jan 2, 2025 · 0 comments

Comments

@Epicguru
Copy link

Epicguru commented Jan 2, 2025

All wrapper sections completely ignore the RESULT value returned by the native library calls.
This makes it impossible to know if some call is not working, for example loading an event that does not exist or setting an invalid configuration, both very common and easy mistakes to make.

This does not only lead to user error, but also the very core wrapper is also not fully functional: for example in the FmodManager class there is a call to setDSPBufferSize that fails every single time, but since the result is not checked nobody saw the problem.

In the current state of the library, the first symptom of an FMOD call not working is typically a hard crash caused by a memory access violation, caused by the wrapper returning a reference to an invalid memory address when it should have checked the result before assuming that the pointer is valid.

I have forked this repo to make adjustments, and I can make a PR if it is wanted. In my fork, at a very minimum I will be adding the following:

/// <summary>
/// An exception thrown when an FMOD function returns an error code.
/// </summary>
public sealed class FModException : Exception
{
	/// <summary>
	/// The result, containing the error code.
	/// </summary>
	public RESULT Result { get; }
	
	public FModException(in RESULT result, string? expression) 
		:base(expression == null ? $"FMOD error: {FMOD.Error.String(result)}" : $"FMOD error: {FMOD.Error.String(result)} ({expression})")
	{
		Result = result;
	}
}

// Helper extension method:
internal static void ThrowIfNotOk(this RESULT result, [CallerArgumentExpression(nameof(result))] string callerLine = "")
{
	if (result != RESULT.OK)
		throw new FModException(result, callerLine);
}

// Example usage:
public void LoadSampleData() => Native.loadSampleData().ThrowIfNotOk();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant