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

Include filename in load exception #36007

Closed
BrennanConroy opened this issue Jan 2, 2020 · 13 comments · Fixed by #51713
Closed

Include filename in load exception #36007

BrennanConroy opened this issue Jan 2, 2020 · 13 comments · Fixed by #51713
Assignees
Labels
area-Extensions-Configuration bug help wanted [up-for-grabs] Good issue for external contributors
Milestone

Comments

@BrennanConroy
Copy link
Member

We spent a few minutes today trying to debug a file load error only to find out that we were trying to fix the wrong file because the exception doesn't say what file failed to load.

https://github.com/aspnet/Extensions/blob/06dcb43bfb372585d3801cfc333a2d9ee9c189ea/src/Configuration/Config.FileExtensions/src/FileConfigurationProvider.cs#L90

cc @jkotalik

@analogrelay analogrelay transferred this issue from dotnet/extensions May 7, 2020
@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added the untriaged New issue has not been triaged by the area owner label May 7, 2020
@Dotnet-GitSync-Bot
Copy link
Collaborator

I couldn't figure out the best area label to add to this issue. Please help me learn by adding exactly one area label.

@analogrelay
Copy link
Contributor

This could be a nice and low-cost 5.0 fix-up, so I'll leave it untriaged for now.

@ericstj ericstj removed the untriaged New issue has not been triaged by the area owner label Jun 18, 2020
@ericstj ericstj added this to the 5.0.0 milestone Jun 18, 2020
@maryamariyan maryamariyan added the help wanted [up-for-grabs] Good issue for external contributors label Jul 1, 2020
@KimKiHyuk
Copy link
Contributor

KimKiHyuk commented Aug 5, 2020

umm.. is it good? like

...
               using (var stream = file.CreateReadStream())
                {
                    try
                    {
                        Load(stream);
                    }
                    catch (FileNotFoundException e)
                    {
                        var error = new StringBuilder($"The configuration file '{Source.Path}' was not found and is not optional.");
                        HandleException(ExceptionDispatchInfo.Capture(error));
                    }
                    catch (Exception e)
                    {
                        HandleException(ExceptionDispatchInfo.Capture(e));
                    }
                }

@BrennanConroy
Copy link
Member Author

Not quite, the original issue wasn't about a file missing, it was about the file load failing because of bad config, in the case of a json file there was invalid json in the file.

@KimKiHyuk
Copy link
Contributor

KimKiHyuk commented Aug 5, 2020

Not quite, the original issue wasn't about a file missing, it was about the file load failing because of bad config, in the case of a json file there was invalid json in the file.

what about checking json file's syntax? (validation)
like

private void Load(bool reload)
{
            var file = Source.FileProvider?.GetFileInfo(Source.Path);
            if (file == null || !file.Exists)
            {
                if (Source.Optional || reload) // Always optional on reload
                {
                    Data = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
                }
                else
                {
                    var error = new StringBuilder($"The configuration file '{Source.Path}' was not found and is not optional.");
                    if (!string.IsNullOrEmpty(file?.PhysicalPath))
                    {
                        error.Append($" The physical path is '{file.PhysicalPath}'.");
                    }
                    HandleException(ExceptionDispatchInfo.Capture(new FileNotFoundException(error.ToString())));
             } 
            //  pseudo code starts here
             else if (isInvalidJsonFormat()) 
             {
                  // throw exception if json file is invalid
                  ....
             }
 
}

@heesunlee9
Copy link

@BrennanConroy, did you solve the problem? It seems like you already solve it.

@danmoseley
Copy link
Member

@maryamariyan still 5.0?

@maryamariyan
Copy link
Member

no it's not a show stopper for 5.0. moving the milestone.

@ericstj
Copy link
Member

ericstj commented Sep 18, 2020

Ensure that the fix for this covers the suggestion from #42337

It looks like it would be very simple to pass the Source.Path to the FileNotFoundException's constructor.

The line in question is this:

HandleException(ExceptionDispatchInfo.Capture(new FileNotFoundException(error.ToString())));

@maryamariyan maryamariyan added enhancement Product code improvement that does NOT require public API changes/additions bug and removed enhancement Product code improvement that does NOT require public API changes/additions labels Oct 1, 2020
@IEvangelist
Copy link
Member

Hey, @maryamariyan and @eerhardt - do you mind if I take this one?

@eerhardt
Copy link
Member

@IEvangelist - it's all yours!

@ericstj
Copy link
Member

ericstj commented Apr 21, 2021

Make sure to consider the constraints that came up in the last PR: #35183

@IEvangelist
Copy link
Member

Make sure to consider the constraints that came up in the last PR: #35183

Thanks, will do.

@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Apr 22, 2021
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Apr 26, 2021
@ghost ghost locked as resolved and limited conversation to collaborators May 26, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-Extensions-Configuration bug help wanted [up-for-grabs] Good issue for external contributors
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants