Only report command errors to sentry and other minor tweaks

This commit is contained in:
Runebaas 2017-11-27 21:57:26 +01:00
parent fb169738fe
commit c4f10f3f7b
No known key found for this signature in database
GPG key ID: 2677AF508D0300D6
6 changed files with 44 additions and 15 deletions

View file

@ -5,6 +5,7 @@ using System.Threading.Tasks;
using AngleSharp; using AngleSharp;
using Discord; using Discord;
using Discord.Commands; using Discord.Commands;
using Discord.Net;
using Geekbot.net.Lib; using Geekbot.net.Lib;
using StackExchange.Redis; using StackExchange.Redis;
@ -80,6 +81,10 @@ namespace Geekbot.net.Commands
} }
await ReplyAsync("That role doesn't seem to exist"); await ReplyAsync("That role doesn't seem to exist");
} }
catch (HttpException e)
{
await Context.Channel.SendMessageAsync("Seems like i don't have enough permission to give roles...");
}
catch (Exception e) catch (Exception e)
{ {
_errorHandler.HandleCommandException(e, Context); _errorHandler.HandleCommandException(e, Context);

View file

@ -27,7 +27,7 @@
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1-dev-00757" /> <PackageReference Include="Serilog.Sinks.Console" Version="3.1.1-dev-00757" />
<PackageReference Include="Serilog.Sinks.Literate" Version="3.0.1-dev-00044" /> <PackageReference Include="Serilog.Sinks.Literate" Version="3.0.1-dev-00044" />
<PackageReference Include="Serilog.Sinks.RollingFile" Version="3.3.1-dev-00771" /> <PackageReference Include="Serilog.Sinks.RollingFile" Version="3.3.1-dev-00771" />
<PackageReference Include="Serilog.Sinks.SentryIO" Version="1.0.3" /> <PackageReference Include="SharpRaven" Version="2.2.0" />
<PackageReference Include="StackExchange.Redis"> <PackageReference Include="StackExchange.Redis">
<Version>1.2.6</Version> <Version>1.2.6</Version>
</PackageReference> </PackageReference>

View file

@ -1,6 +1,12 @@
using System; using System;
using System.Runtime.InteropServices.ComTypes;
using System.Security.Principal;
using Discord.Commands; using Discord.Commands;
using Nancy.Extensions;
using Serilog; using Serilog;
using SharpRaven;
using SharpRaven.Data;
using SharpRaven.Utilities;
using Utf8Json; using Utf8Json;
namespace Geekbot.net.Lib namespace Geekbot.net.Lib
@ -9,11 +15,23 @@ namespace Geekbot.net.Lib
{ {
private readonly ILogger _logger; private readonly ILogger _logger;
private readonly ITranslationHandler _translation; private readonly ITranslationHandler _translation;
private readonly IRavenClient _raven;
public ErrorHandler(ILogger logger, ITranslationHandler translation) public ErrorHandler(ILogger logger, ITranslationHandler translation)
{ {
_logger = logger; _logger = logger;
_translation = translation; _translation = translation;
var sentryDsn = Environment.GetEnvironmentVariable("SENTRY");
if (!string.IsNullOrEmpty(sentryDsn))
{
_raven = new RavenClient(sentryDsn);
_logger.Information($"Command Errors will be logged to Sentry: {sentryDsn}");
}
else
{
_raven = null;
}
} }
public void HandleCommandException(Exception e, ICommandContext Context, string errorMessage = "def") public void HandleCommandException(Exception e, ICommandContext Context, string errorMessage = "def")
@ -55,6 +73,20 @@ namespace Geekbot.net.Lib
{ {
Context.Channel.SendMessageAsync(errorString); Context.Channel.SendMessageAsync(errorString);
} }
if (_raven == null) return;
var sentryEvent = new SentryEvent(e)
{
Tags =
{
["discord_server"] = errorObj.Guild.Name,
["discord_user"] = errorObj.User.Name
},
Message = errorObj.Message.Content,
Extra = errorObj
};
_raven.Capture(sentryEvent);
} }
catch (Exception ex) catch (Exception ex)
{ {

View file

@ -11,13 +11,6 @@ namespace Geekbot.net.Lib
var loggerCreation = new LoggerConfiguration() var loggerCreation = new LoggerConfiguration()
.WriteTo.LiterateConsole() .WriteTo.LiterateConsole()
.WriteTo.RollingFile("Logs/geekbot-{Date}.txt", shared: true); .WriteTo.RollingFile("Logs/geekbot-{Date}.txt", shared: true);
var sentryDsn = Environment.GetEnvironmentVariable("SENTRY");
if (!string.IsNullOrEmpty(sentryDsn))
{
loggerCreation.WriteTo.SentryIO(sentryDsn)
.Enrich.FromLogContext();
Console.WriteLine($"Logging to Sentry Enabled: {sentryDsn}");
}
if (args.Contains("--verbose")) if (args.Contains("--verbose"))
{ {
loggerCreation.MinimumLevel.Verbose(); loggerCreation.MinimumLevel.Verbose();

View file

@ -195,7 +195,7 @@ namespace Geekbot.net
{ {
redis.StringSet("botOwner", appInfo.Owner.Id); redis.StringSet("botOwner", appInfo.Owner.Id);
var req = HttpWebRequest.Create(appInfo.IconUrl); var req = HttpWebRequest.Create(appInfo.IconUrl);
using (Stream stream = req.GetResponse().GetResponseStream()) using (var stream = req.GetResponse().GetResponseStream())
{ {
await client.CurrentUser.ModifyAsync(User => await client.CurrentUser.ModifyAsync(User =>
{ {
@ -207,8 +207,8 @@ namespace Geekbot.net
} }
catch (Exception e) catch (Exception e)
{ {
logger.Warning(e, $"[Setup] Oha, it seems like something went wrong while running the setup"); logger.Warning(e, "[Setup] Oha, it seems like something went wrong while running the setup");
logger.Warning(e, $"[Setup] Geekbot will work never the less, some features might be disabled though"); logger.Warning("[Setup] Geekbot will work never the less, some features might be disabled though");
} }
return Task.CompletedTask; return Task.CompletedTask;
} }

View file

@ -1,4 +1,4 @@
using System; using System.Diagnostics;
using Nancy; using Nancy;
using Nancy.Bootstrapper; using Nancy.Bootstrapper;
using Nancy.TinyIoc; using Nancy.TinyIoc;
@ -11,13 +11,12 @@ namespace Geekbot.net.WebApi
{ {
//CORS Enable //CORS Enable
pipelines.AfterRequest.AddItemToEndOfPipeline((ctx) => pipelines.AfterRequest.AddItemToEndOfPipeline(ctx =>
{ {
ctx.Response.WithHeader("Access-Control-Allow-Origin", "*") ctx.Response.WithHeader("Access-Control-Allow-Origin", "*")
.WithHeader("Access-Control-Allow-Methods", "GET") .WithHeader("Access-Control-Allow-Methods", "GET")
.WithHeader("Access-Control-Allow-Headers", "Accept, Origin, Content-type") .WithHeader("Access-Control-Allow-Headers", "Accept, Origin, Content-type")
.WithHeader("Last-Modified", DateTime.Now.ToString()); .WithHeader("Last-Modified", Process.GetCurrentProcess().StartTime.ToString());
}); });
} }
} }