Skip to content

Commit

Permalink
Add Try/Catch block around saving trade codes to the file.
Browse files Browse the repository at this point in the history
  • Loading branch information
bdawg1989 committed Jun 20, 2024
1 parent 0aa1ce0 commit ad2926f
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions SysBot.Pokemon/Queues/TradeCodeStorage.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using SysBot.Base;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
Expand All @@ -7,13 +9,11 @@ namespace SysBot.Pokemon;
public class TradeCodeStorage
{
private const string FileName = "tradecodes.json";

private static readonly JsonSerializerOptions SerializerOptions = new()
{
PropertyNameCaseInsensitive = true,
WriteIndented = true
};

private Dictionary<ulong, TradeCodeDetails>? _tradeCodeDetails;

public TradeCodeStorage() => LoadFromFile();
Expand Down Expand Up @@ -97,20 +97,31 @@ private void LoadFromFile()

private void SaveToFile()
{
string json = JsonSerializer.Serialize(_tradeCodeDetails, SerializerOptions);
File.WriteAllText(FileName, json);
try
{
string json = JsonSerializer.Serialize(_tradeCodeDetails, SerializerOptions);
File.WriteAllText(FileName, json);
}
catch (IOException ex)
{
LogUtil.LogInfo("TradeCodeStorage", $"Error saving trade codes to file: {ex.Message}");
}
catch (UnauthorizedAccessException ex)
{
LogUtil.LogInfo("TradeCodeStorage", $"Access denied while saving trade codes to file: {ex.Message}");
}
catch (Exception ex)
{
LogUtil.LogInfo("TradeCodeStorage", $"An error occurred while saving trade codes to file: {ex.Message}");
}
}

public class TradeCodeDetails
{
public int Code { get; set; }

public string? OT { get; set; }

public int SID { get; set; }

public int TID { get; set; }

public int TradeCount { get; set; }
}
}

0 comments on commit ad2926f

Please sign in to comment.