2017-09-26 22:09:57 +02:00
|
|
|
|
using System;
|
2017-11-27 22:07:05 +01:00
|
|
|
|
using System.Net;
|
2017-11-27 21:57:26 +01:00
|
|
|
|
using System.Runtime.InteropServices.ComTypes;
|
|
|
|
|
using System.Security.Principal;
|
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 Nancy.Extensions;
|
2017-09-26 22:09:57 +02:00
|
|
|
|
using Serilog;
|
2017-11-27 21:57:26 +01:00
|
|
|
|
using SharpRaven;
|
|
|
|
|
using SharpRaven.Data;
|
|
|
|
|
using SharpRaven.Utilities;
|
2017-11-14 23:08:36 +01:00
|
|
|
|
using Utf8Json;
|
2017-09-26 22:09:57 +02:00
|
|
|
|
|
|
|
|
|
namespace Geekbot.net.Lib
|
|
|
|
|
{
|
|
|
|
|
public class ErrorHandler : IErrorHandler
|
|
|
|
|
{
|
2017-10-19 21:43:37 +02:00
|
|
|
|
private readonly ILogger _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;
|
2017-09-26 22:09:57 +02:00
|
|
|
|
|
2017-11-16 17:19:43 +01:00
|
|
|
|
public ErrorHandler(ILogger logger, ITranslationHandler translation)
|
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;
|
2017-11-27 21:57:26 +01:00
|
|
|
|
|
|
|
|
|
var sentryDsn = Environment.GetEnvironmentVariable("SENTRY");
|
|
|
|
|
if (!string.IsNullOrEmpty(sentryDsn))
|
|
|
|
|
{
|
|
|
|
|
_raven = new RavenClient(sentryDsn);
|
2017-11-27 22:07:05 +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
|
|
|
|
}
|
|
|
|
|
|
2017-11-16 17:19:43 +01: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
|
|
|
|
{
|
2017-11-16 17:19:43 +01:00
|
|
|
|
var errorString = errorMessage == "def" ? _translation.GetString(Context.Guild.Id, "errorHandler", "SomethingWentWrong") : errorMessage;
|
2017-11-14 23:08:36 +01:00
|
|
|
|
var errorObj = new ErrorObject()
|
|
|
|
|
{
|
|
|
|
|
Message = new ErrorMessage()
|
|
|
|
|
{
|
|
|
|
|
Content = Context.Message.Content,
|
|
|
|
|
Id = Context.Message.Id.ToString(),
|
|
|
|
|
Attachments = Context.Message.Attachments.Count,
|
|
|
|
|
ChannelMentions = Context.Message.MentionedChannelIds.Count,
|
|
|
|
|
UserMentions = Context.Message.MentionedUserIds.Count,
|
|
|
|
|
RoleMentions = Context.Message.MentionedRoleIds.Count
|
|
|
|
|
},
|
|
|
|
|
User = new IdAndName()
|
|
|
|
|
{
|
|
|
|
|
Id = Context.User.Id.ToString(),
|
|
|
|
|
Name = $"{Context.User.Username}#{Context.User.Discriminator}"
|
|
|
|
|
},
|
|
|
|
|
Guild = new IdAndName()
|
|
|
|
|
{
|
|
|
|
|
Id = Context.Guild.Id.ToString(),
|
|
|
|
|
Name = Context.Guild.Name
|
|
|
|
|
},
|
|
|
|
|
Channel = new IdAndName()
|
|
|
|
|
{
|
|
|
|
|
Id = Context.Channel.Id.ToString(),
|
|
|
|
|
Name = Context.Channel.Name
|
|
|
|
|
},
|
|
|
|
|
TimeStamp = DateTime.Now.ToString()
|
|
|
|
|
};
|
|
|
|
|
var errorJson = JsonSerializer.ToJsonString(errorObj);
|
|
|
|
|
_logger.Error(e, errorJson);
|
|
|
|
|
if (!string.IsNullOrEmpty(errorMessage))
|
|
|
|
|
{
|
2017-11-16 17:19:43 +01:00
|
|
|
|
Context.Channel.SendMessageAsync(errorString);
|
2017-11-14 23:08:36 +01:00
|
|
|
|
}
|
2017-11-27 21:57:26 +01:00
|
|
|
|
|
|
|
|
|
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);
|
2017-09-28 18:55:57 +02:00
|
|
|
|
}
|
2017-11-14 23:08:36 +01:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.Error(ex, "Errorception");
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-11-27 22:07:05 +01:00
|
|
|
|
|
|
|
|
|
public async void HandleHttpException(HttpException e, ICommandContext Context)
|
|
|
|
|
{
|
|
|
|
|
var errorStrings = _translation.GetDict(Context, "httpErrors");
|
|
|
|
|
switch(e.HttpCode)
|
|
|
|
|
{
|
|
|
|
|
case HttpStatusCode.Forbidden:
|
|
|
|
|
await Context.Channel.SendMessageAsync(errorStrings["403"]);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-11-14 23:08:36 +01:00
|
|
|
|
public class ErrorObject
|
|
|
|
|
{
|
|
|
|
|
public ErrorMessage Message { get; set; }
|
|
|
|
|
public IdAndName User { get; set; }
|
|
|
|
|
public IdAndName Guild { get; set; }
|
|
|
|
|
public IdAndName Channel { get; set; }
|
|
|
|
|
public string TimeStamp { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class ErrorMessage
|
|
|
|
|
{
|
|
|
|
|
public string Content { get; set; }
|
|
|
|
|
public string Id { get; set; }
|
|
|
|
|
public int Attachments { get; set; }
|
|
|
|
|
public int ChannelMentions { get; set; }
|
|
|
|
|
public int UserMentions { get; set; }
|
|
|
|
|
public int RoleMentions { get; set; }
|
|
|
|
|
}
|
2017-10-19 21:43:37 +02:00
|
|
|
|
|
2017-11-14 23:08:36 +01:00
|
|
|
|
public class IdAndName
|
|
|
|
|
{
|
|
|
|
|
public string Id { get; set; }
|
|
|
|
|
public string Name { get; set; }
|
2017-09-26 22:09:57 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public interface IErrorHandler
|
|
|
|
|
{
|
2017-11-16 17:19:43 +01:00
|
|
|
|
void HandleCommandException(Exception e, ICommandContext Context, string errorMessage = "def");
|
2017-11-27 22:07:05 +01:00
|
|
|
|
void HandleHttpException(HttpException e, ICommandContext Context);
|
2017-09-26 22:09:57 +02:00
|
|
|
|
}
|
|
|
|
|
}
|