Skip to content

Commit

Permalink
Ajout de méthode synchrone necessaire pour pouvoir envoyer la requête…
Browse files Browse the repository at this point in the history
… lors de réception d’exception non capturée
  • Loading branch information
Julien Binet committed Nov 24, 2016
1 parent 95a7324 commit c488206
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 14 deletions.
8 changes: 6 additions & 2 deletions RollbarDotNet/IRollbar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ public interface IRollbar
{
void PersonData(Func<Person> personFunc);

Task<Guid?> Report(System.Exception e, ErrorLevel? level = ErrorLevel.Error, IDictionary<string, object> custom = null);
Task<Guid?> ReportAsync(Exception e, ErrorLevel? level = ErrorLevel.Error, IDictionary<string, object> custom = null);

Task<Guid?> Report(string message, ErrorLevel? level = ErrorLevel.Error, IDictionary<string, object> custom = null);
Task<Guid?> ReportAsync(string message, ErrorLevel? level = ErrorLevel.Error, IDictionary<string, object> custom = null);

Guid? Report(Exception e, ErrorLevel? level = ErrorLevel.Error, IDictionary<string, object> custom = null);

Guid? Report(string message, ErrorLevel? level = ErrorLevel.Error, IDictionary<string, object> custom = null);
}
}
26 changes: 25 additions & 1 deletion RollbarDotNet/RollbarClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ public async Task<Guid> PostItemAsync(Payload payload)
{
var stringResult = await SendPostAsync("item/", payload);
return ParseResponse(stringResult);
}

public Guid PostItem(Payload payload)
{
var stringResult = SendPost("item/", payload);
return ParseResponse(stringResult);
}

static Guid ParseResponse(string stringResult)
Expand Down Expand Up @@ -54,7 +60,7 @@ async Task<string> SendPostAsync<T>(string url, T payload)
try
{
var httpClient = new HttpClient();
httpClient.BaseAddress = (new Uri(Config.EndPoint));
httpClient.BaseAddress = (new Uri(Config.EndPoint));

var result = await httpClient.PostAsync(url, new StringContent(JsonConvert.SerializeObject(payload)));
return await result.Content.ReadAsStringAsync();
Expand All @@ -66,5 +72,23 @@ async Task<string> SendPostAsync<T>(string url, T payload)
return string.Empty;
}
}

string SendPost<T>(string url, T payload)
{
try
{
var httpClient = new HttpClient();
httpClient.BaseAddress = (new Uri(Config.EndPoint));

var result = httpClient.PostAsync(url, new StringContent(JsonConvert.SerializeObject(payload))).Result;
return result.Content.ReadAsStringAsync().Result;
}
catch (Exception ex)
{
Debug.WriteLine("[RollbarDotNet] The request sent to the api failed.");
Debug.WriteLine(ex);
return string.Empty;
}
}
}
}
60 changes: 49 additions & 11 deletions RollbarDotNet/RollbarImplementation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,38 +22,76 @@ public void PersonData(Func<Person> personFunc)
_personFunc = personFunc;
}

public async Task<Guid?> Report(Exception e, ErrorLevel? level = ErrorLevel.Error, IDictionary<string, object> custom = null)
public async Task<Guid?> ReportAsync(Exception e, ErrorLevel? level = ErrorLevel.Error, IDictionary<string, object> custom = null)
{
return await SendBody(new Body(e), level, custom);
if (!IsCanSend)
return null;

var payload = BuildPayload(new Body(e), level, custom);
return await SendBodyAsync(payload);
}

public async Task<Guid?> Report(string message, ErrorLevel? level = ErrorLevel.Error, IDictionary<string, object> custom = null)
public async Task<Guid?> ReportAsync(string message, ErrorLevel? level = ErrorLevel.Error, IDictionary<string, object> custom = null)
{
return await SendBody(new Body(new Message(message)), level, custom);
if (!IsCanSend)
return null;

var payload = BuildPayload(new Body(new Message(message)), level, custom);
return await SendBodyAsync(payload);
}

static async Task<Guid?> SendBody(Body body, ErrorLevel? level, IDictionary<string, object> custom)
public Guid? Report(Exception e, ErrorLevel? level = ErrorLevel.Error, IDictionary<string, object> custom = null)
{
if (string.IsNullOrWhiteSpace(_config.AccessToken) || _config.Enabled == false)
if (!IsCanSend)
return null;

var guid = Guid.NewGuid();
var payload = BuildPayload(new Body(e), level, custom);
return SendBody(payload);
}

var client = new RollbarClient(_config);
public Guid? Report(string message, ErrorLevel? level = ErrorLevel.Error, IDictionary<string, object> custom = null)
{
if (!IsCanSend)
return null;

var payload = BuildPayload(new Body(new Message(message)), level, custom);
return SendBody(payload);
}

static bool IsCanSend
{
get { return !string.IsNullOrWhiteSpace(_config.AccessToken) && _config.Enabled == true; }
}

static Payload BuildPayload(Body body, ErrorLevel? level, IDictionary<string, object> custom)
{
var data = new Data(_config.Environment, body)
{
Custom = custom,
Level = level ?? _config.LogLevel
};

var payload = new Payload(_config.AccessToken, data);
payload.Data.GuidUuid = guid;
payload.Data.GuidUuid = Guid.NewGuid();
payload.Data.Person = _personFunc?.Invoke();

_config.Transform?.Invoke(payload);
return payload;
}

static async Task<Guid?> SendBodyAsync(Payload payload)
{
var client = new RollbarClient(_config);
await client.PostItemAsync(payload);

return guid;
return payload.Data.GuidUuid;
}

static Guid? SendBody(Payload payload)
{
var client = new RollbarClient(_config);
client.PostItem(payload);

return payload.Data.GuidUuid;
}
}
}

0 comments on commit c488206

Please sign in to comment.