geekbot/Geekbot.net/Lib/ErrorHandling/ErrorHandler.cs

107 lines
4 KiB
C#
Raw Normal View History

2017-09-26 22:09:57 +02:00
using System;
2017-11-27 22:07:05 +01:00
using System.Net;
using System.Threading.Tasks;
2017-09-26 22:09:57 +02:00
using Discord.Commands;
2017-11-27 22:07:05 +01:00
using Discord.Net;
2018-09-11 00:13:54 +02:00
using Geekbot.net.Lib.GlobalSettings;
using Geekbot.net.Lib.Localization;
using Geekbot.net.Lib.Logger;
using SharpRaven;
using SharpRaven.Data;
2018-09-11 00:13:54 +02:00
using Exception = System.Exception;
2017-09-26 22:09:57 +02:00
namespace Geekbot.net.Lib.ErrorHandling
2017-09-26 22:09:57 +02:00
{
public class ErrorHandler : IErrorHandler
{
private readonly IGeekbotLogger _logger;
private readonly ITranslationHandler _translation;
private readonly IRavenClient _raven;
private readonly bool _errorsInChat;
2017-09-26 22:09:57 +02:00
2018-09-11 00:13:54 +02:00
public ErrorHandler(IGeekbotLogger logger, ITranslationHandler translation, IGlobalSettings globalSettings, bool errorsInChat)
2017-09-26 22:09:57 +02:00
{
2017-10-19 21:43:37 +02:00
_logger = logger;
_translation = translation;
_errorsInChat = errorsInChat;
var sentryDsn = Environment.GetEnvironmentVariable("SENTRY");
if (!string.IsNullOrEmpty(sentryDsn))
{
_raven = new RavenClient(sentryDsn) { Release = Constants.BotVersion(), Environment = "Production" };
_logger.Information(LogSource.Geekbot, $"Command Errors will be logged to Sentry: {sentryDsn}");
}
else
{
_raven = null;
}
2017-09-26 22:09:57 +02:00
}
public async Task HandleCommandException(Exception e, ICommandContext context, string errorMessage = "def")
2017-09-26 22:09:57 +02:00
{
try
2017-09-28 18:55:57 +02:00
{
var errorString = errorMessage == "def" ? await _translation.GetString(context.Guild?.Id ?? 0, "errorHandler", "SomethingWentWrong") : errorMessage;
2018-04-30 23:44:19 +02:00
var errorObj = SimpleConextConverter.ConvertContext(context);
if (e.Message.Contains("50007")) return;
if (e.Message.Contains("50013")) return;
_logger.Error(LogSource.Geekbot, "An error ocured", e, errorObj);
if (!string.IsNullOrEmpty(errorMessage))
{
if (_errorsInChat)
{
var resStackTrace = string.IsNullOrEmpty(e.InnerException?.ToString()) ? e.StackTrace : e.InnerException?.ToString();
if (!string.IsNullOrEmpty(resStackTrace))
{
var maxLen = Math.Min(resStackTrace.Length, 1850);
2018-07-28 16:31:18 +02:00
await context.Channel.SendMessageAsync($"{e.Message}\r\n```\r\n{resStackTrace.Substring(0, maxLen)}\r\n```");
}
else
{
2018-07-28 16:31:18 +02:00
await context.Channel.SendMessageAsync(e.Message);
}
}
else
{
2018-07-28 16:31:18 +02:00
await context.Channel.SendMessageAsync(errorString);
}
}
2018-09-11 00:13:54 +02:00
ReportExternal(e, errorObj);
2017-09-28 18:55:57 +02:00
}
catch (Exception ex)
{
2018-07-28 16:31:18 +02:00
await context.Channel.SendMessageAsync("Something went really really wrong here");
_logger.Error(LogSource.Geekbot, "Errorception", ex);
}
}
2017-11-27 22:07:05 +01:00
public async Task HandleHttpException(HttpException e, ICommandContext context)
2017-11-27 22:07:05 +01:00
{
var errorStrings = await _translation.GetDict(context, "httpErrors");
2017-11-27 22:07:05 +01:00
switch(e.HttpCode)
{
case HttpStatusCode.Forbidden:
2018-04-30 23:44:19 +02:00
await context.Channel.SendMessageAsync(errorStrings["403"]);
2017-11-27 22:07:05 +01:00
break;
}
}
2018-09-11 00:13:54 +02:00
private void ReportExternal(Exception e, MessageDto errorObj)
{
2019-03-02 06:50:54 +01:00
if (_raven == null) return;
var sentryEvent = new SentryEvent(e)
2018-09-11 00:13:54 +02:00
{
2019-03-02 06:50:54 +01:00
Tags =
2018-09-11 00:13:54 +02:00
{
2019-03-02 06:50:54 +01:00
["discord_server"] = errorObj.Guild.Name,
["discord_user"] = errorObj.User.Name
},
Message = errorObj.Message.Content,
Extra = errorObj
};
_raven.Capture(sentryEvent);
2018-09-11 00:13:54 +02:00
}
2017-09-26 22:09:57 +02:00
}
}