0

I'm wondering whether there is a clean way to create a new file using Octokit in case the file specified doesn't exist, or otherwise create it? I'm currently attempting to do this via a try-catch block. However, there needs to be a more straightforward way to do this except a try-catch block?

    private IRepositoryContentsClient Content => _client.Repository.Content;

    public void TransmitLog(string logFilePath)
    {
        var logContent = LogFileToString(logFilePath);
        var githubPath = GenerateGithubPath(logFilePath);
        try
        {
            UpdateLog(githubPath, logContent);
        }
        catch (NotFoundException)
        {
            CreateLog(githubPath, logContent);
        }
        catch (AggregateException) 
        {
            CreateLog(githubPath, logContent);
        }
    }

    private void UpdateLog(string githubPath, string logContent)
    {
        // MY APP FAILS HERE
        var existingFile = Content.GetAllContentsByRef(
            _owner, _repo, githubPath, _branch).Result;
        // update the file
        var relevantSha = existingFile.First().Sha;
        var updateRequest = new UpdateFileRequest("Log update" + DateTime.UtcNow, logContent, relevantSha, _branch);
        var updateChangeSet = Content.UpdateFile(_owner, _repo, githubPath, updateRequest);
    }

    private void CreateLog(string githubPath, string logFileContent)
    {
        // if file is not found, create it
        var createRequest =
            new CreateFileRequest("Log Creation" + DateTime.UtcNow, logFileContent, _branch);
        var createChangeSet = Content.CreateFile(_owner, _repo, githubPath, createRequest);
    }

EDIT: I initially had both CreateLog and UpdateLog declared as async void, which led to a whole set of other problems. I edited that out since what I'm really interested in is how to avoid this try/catch structure.

emilaz
  • 1,722
  • 1
  • 15
  • 31
  • 1
    [Avoid Async Void](https://learn.microsoft.com/en-us/archive/msdn-magazine/2013/march/async-await-best-practices-in-asynchronous-programming#avoid-async-void) – Luke Jan 31 '23 at 16:26
  • Ugh thanks, didn't know about this. I'll edit my post and take out the async call, as the proposed design isn't depending on sync/async and that is at the core of my question. – emilaz Jan 31 '23 at 16:29
  • Hrm, that seems like it's not really going to help since Content.UpdateFile is still going to be async. Any reason you couldn't make everything (including TransmitLog) `async Task` and await all the way up? That would make your exception handling work as desired – Luke Jan 31 '23 at 16:34
  • Sounds reasonable and I think I'll go wit that for now. However, my question is really trying to take a step back and trying to understand whether Octokit might offer a functionality that does what I want without the need for a try/catch block. For example, I'd expect the `GetAllContentsByRef` function to simply return null if nothing was found instead of throwing an error, like it currently does. Maybe there is a separate function that would do that? – emilaz Jan 31 '23 at 16:41

0 Answers0