2017-09-26 22:09:57 +02:00
|
|
|
|
using System;
|
2017-11-27 22:07:05 +01:00
|
|
|
|
using System.Net;
|
2017-09-26 22:09:57 +02:00
|
|
|
|
using Discord.Commands;
|
2017-11-27 22:07:05 +01:00
|
|
|
|
using Discord.Net;
|
2017-11-27 21:57:26 +01:00
|
|
|
|
using SharpRaven;
|
|
|
|
|
using SharpRaven.Data;
|
2017-09-26 22:09:57 +02:00
|
|
|
|
|
|
|
|
|
namespace Geekbot.net.Lib
|
|
|
|
|
{
|
|
|
|
|
public class ErrorHandler : IErrorHandler
|
|
|
|
|
{
|
2018-01-20 01:38:49 +01:00
|
|
|
|
private readonly IGeekbotLogger _logger;
|
2017-11-16 17:19:43 +01:00
|
|
|
|
private readonly ITranslationHandler _translation;
|
2017-11-27 21:57:26 +01:00
|
|
|
|
private readonly IRavenClient _raven;
|
2018-04-28 01:01:48 +02:00
|
|
|
|
private readonly bool _errorsInChat;
|
2017-09-26 22:09:57 +02:00
|
|
|
|
|
2018-04-28 01:01:48 +02:00
|
|
|
|
public ErrorHandler(IGeekbotLogger logger, ITranslationHandler translation, bool errorsInChat)
|
2017-09-26 22:09:57 +02:00
|
|
|
|
{
|
2017-10-19 21:43:37 +02:00
|
|
|
|
_logger = logger;
|
2017-11-16 17:19:43 +01:00
|
|
|
|
_translation = translation;
|
2018-04-28 01:01:48 +02:00
|
|
|
|
_errorsInChat = errorsInChat;
|
|
|
|
|
|
2017-11-27 21:57:26 +01:00
|
|
|
|
var sentryDsn = Environment.GetEnvironmentVariable("SENTRY");
|
|
|
|
|
if (!string.IsNullOrEmpty(sentryDsn))
|
|
|
|
|
{
|
|
|
|
|
_raven = new RavenClient(sentryDsn);
|
2018-01-20 01:38:49 +01:00
|
|
|
|
_logger.Information("Geekbot", $"Command Errors will be logged to Sentry: {sentryDsn}");
|
2017-11-27 21:57:26 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_raven = null;
|
|
|
|
|
}
|
2017-09-26 22:09:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-30 23:44:19 +02:00
|
|
|
|
public void HandleCommandException(Exception e, ICommandContext context, string errorMessage = "def")
|
2017-09-26 22:09:57 +02:00
|
|
|
|
{
|
2017-11-14 23:08:36 +01:00
|
|
|
|
try
|
2017-09-28 18:55:57 +02:00
|
|
|
|
{
|
2018-04-30 23:44:19 +02:00
|
|
|
|
var errorString = errorMessage == "def" ? _translation.GetString(context.Guild.Id, "errorHandler", "SomethingWentWrong") : errorMessage;
|
|
|
|
|
var errorObj = SimpleConextConverter.ConvertContext(context);
|
2018-02-04 17:36:55 +01:00
|
|
|
|
if (e.Message.Contains("50007")) return;
|
|
|
|
|
if (e.Message.Contains("50013")) return;
|
2018-01-20 01:38:49 +01:00
|
|
|
|
_logger.Error("Geekbot", "An error ocured", e, errorObj);
|
2017-11-14 23:08:36 +01:00
|
|
|
|
if (!string.IsNullOrEmpty(errorMessage))
|
|
|
|
|
{
|
2018-04-28 01:01:48 +02:00
|
|
|
|
if (_errorsInChat)
|
|
|
|
|
{
|
2018-04-28 02:46:30 +02:00
|
|
|
|
var resStackTrace = string.IsNullOrEmpty(e.InnerException?.ToString()) ? e.StackTrace : e.InnerException.ToString();
|
|
|
|
|
var maxLen = Math.Min(resStackTrace.Length, 1850);
|
2018-04-30 23:44:19 +02:00
|
|
|
|
context.Channel.SendMessageAsync($"{e.Message}\r\n```\r\n{resStackTrace.Substring(0, maxLen)}\r\n```");
|
2018-04-28 01:01:48 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-04-30 23:44:19 +02:00
|
|
|
|
context.Channel.SendMessageAsync(errorString);
|
2018-04-28 01:01:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-14 23:08:36 +01:00
|
|
|
|
}
|
2017-11-27 21:57:26 +01:00
|
|
|
|
|
|
|
|
|
if (_raven == null) return;
|
2018-02-04 14:47:10 +01:00
|
|
|
|
|
2017-11-27 21:57:26 +01:00
|
|
|
|
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);
|
2017-09-28 18:55:57 +02:00
|
|
|
|
}
|
2017-11-14 23:08:36 +01:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2018-04-30 23:44:19 +02:00
|
|
|
|
context.Channel.SendMessageAsync("Something went really really wrong here");
|
2018-01-20 01:38:49 +01:00
|
|
|
|
_logger.Error("Geekbot", "Errorception", ex);
|
2017-11-14 23:08:36 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-11-27 22:07:05 +01:00
|
|
|
|
|
2018-04-30 23:44:19 +02:00
|
|
|
|
public async void HandleHttpException(HttpException e, ICommandContext context)
|
2017-11-27 22:07:05 +01:00
|
|
|
|
{
|
2018-04-30 23:44:19 +02:00
|
|
|
|
var errorStrings = _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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-14 23:08:36 +01:00
|
|
|
|
|
2017-09-26 22:09:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public interface IErrorHandler
|
|
|
|
|
{
|
2018-04-30 23:44:19 +02:00
|
|
|
|
void HandleCommandException(Exception e, ICommandContext context, string errorMessage = "def");
|
|
|
|
|
void HandleHttpException(HttpException e, ICommandContext context);
|
2017-09-26 22:09:57 +02:00
|
|
|
|
}
|
|
|
|
|
}
|