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

Throw an exception when the parameter is null #36

Closed
k3ndo opened this issue Feb 13, 2022 · 0 comments
Closed

Throw an exception when the parameter is null #36

k3ndo opened this issue Feb 13, 2022 · 0 comments
Labels
enhancement Improve a feature or component of the library
Milestone

Comments

@k3ndo
Copy link
Contributor

k3ndo commented Feb 13, 2022

It would be nice if some methods would throw ArgumentNullException when the parameter is null. Some methods expect other methods to throw ArgumentNullException but the disadvantage of this is that it is not clear why the error occurred.

For example:

new EnvLoader()
     .SetDefaultEnvFileName(Environment.GetEnvironmentVariable("DEFAULT"))
     .Load();

Output:

Unhandled exception. System.ArgumentNullException: Value cannot be null. (Parameter 'path1')
   at System.IO.Path.Combine(String path1, String path2)
   at DotEnv.Core.EnvLoader.SetConfigurationEnvFile(EnvFile envFile) in C:\Users\syslan\Documents\Visual Studio 2022\DotEnv.Core\src\Loader\EnvLoader.HelperMethods.cs:line 64
   at DotEnv.Core.EnvLoader.Load(EnvValidationResult& result) in C:\Users\syslan\Documents\Visual Studio 2022\DotEnv.Core\src\Loader\EnvLoader.cs:line 59
   at DotEnv.Core.EnvLoader.Load() in C:\Users\syslan\Documents\Visual Studio 2022\DotEnv.Core\src\Loader\EnvLoader.cs:line 49
   at DotEnv.Core.Example.Program.Main(String[] args) in C:\Users\syslan\Documents\Visual Studio 2022\DotEnv.Core\example\Program.cs:line 55

According to this output, the parameter Path1 is the one that caused the exception but this information is confusing for the consumer because it does not really tell us the cause of the error. If we guess the error it is because a null value is being passed to the SetDefaultEnvFileName method but this information is not told in the exception message, so it is confusing.

This message would be clearer to the consumer:

Unhandled exception. System.ArgumentNullException: Value cannot be null. (Parameter 'envFileName')
   at DotEnv.Core.EnvLoader.SetDefaultEnvFileName(String envFileName) in C:\Users\syslan\Documents\Visual Studio 2022\DotEnv.Core\src\Loader\EnvLoader.cs:line 49
   at DotEnv.Core.Example.Program.Main(String[] args) in C:\Users\syslan\Documents\Visual Studio 2022\DotEnv.Core\example\Program.cs:line 55

It is clear that the above message is much more explicit for the consumer because it tells us why the error occurred and it was because the null value was passed to the SetDefaultEnvFileName method.

I will create a PR for this.

@MrDave1999 MrDave1999 added the feature New feature or request label Feb 16, 2022
@MrDave1999 MrDave1999 added this to the 8.0.0 milestone Apr 10, 2022
@MrDave1999 MrDave1999 added enhancement Improve a feature or component of the library and removed feature New feature or request labels Apr 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improve a feature or component of the library
Projects
None yet
Development

No branches or pull requests

2 participants