From 53debf9c9d04e511b9b8e2f111ec304421bafaa1 Mon Sep 17 00:00:00 2001 From: Runebaas Date: Mon, 27 Nov 2017 22:07:05 +0100 Subject: [PATCH] Add httpErrorHandler --- Geekbot.net/Commands/Role.cs | 2 +- Geekbot.net/Lib/ErrorHandler.cs | 19 +++++++++++++++++-- Geekbot.net/Storage/Translations.json | 6 ++++++ 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/Geekbot.net/Commands/Role.cs b/Geekbot.net/Commands/Role.cs index 851ff02..f40ccf1 100644 --- a/Geekbot.net/Commands/Role.cs +++ b/Geekbot.net/Commands/Role.cs @@ -83,7 +83,7 @@ namespace Geekbot.net.Commands } catch (HttpException e) { - await Context.Channel.SendMessageAsync("Seems like i don't have enough permission to give roles..."); + _errorHandler.HandleHttpException(e, Context); } catch (Exception e) { diff --git a/Geekbot.net/Lib/ErrorHandler.cs b/Geekbot.net/Lib/ErrorHandler.cs index 109ba03..1d6ffd8 100644 --- a/Geekbot.net/Lib/ErrorHandler.cs +++ b/Geekbot.net/Lib/ErrorHandler.cs @@ -1,7 +1,9 @@ using System; +using System.Net; using System.Runtime.InteropServices.ComTypes; using System.Security.Principal; using Discord.Commands; +using Discord.Net; using Nancy.Extensions; using Serilog; using SharpRaven; @@ -26,7 +28,7 @@ namespace Geekbot.net.Lib if (!string.IsNullOrEmpty(sentryDsn)) { _raven = new RavenClient(sentryDsn); - _logger.Information($"Command Errors will be logged to Sentry: {sentryDsn}"); + _logger.Information($"[Geekbot] Command Errors will be logged to Sentry: {sentryDsn}"); } else { @@ -93,7 +95,19 @@ namespace Geekbot.net.Lib _logger.Error(ex, "Errorception"); } } - + + 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; + } + } + + public class ErrorObject { public ErrorMessage Message { get; set; } @@ -123,5 +137,6 @@ namespace Geekbot.net.Lib public interface IErrorHandler { void HandleCommandException(Exception e, ICommandContext Context, string errorMessage = "def"); + void HandleHttpException(HttpException e, ICommandContext Context); } } \ No newline at end of file diff --git a/Geekbot.net/Storage/Translations.json b/Geekbot.net/Storage/Translations.json index ad02cba..93a7421 100644 --- a/Geekbot.net/Storage/Translations.json +++ b/Geekbot.net/Storage/Translations.json @@ -15,6 +15,12 @@ "CHDE": "Öppis isch schief gange :confused:" } }, + "httpErrors": { + "403": { + "EN": "Seems like i don't have enough permission to that :confused:", + "CHDE": "Gseht danach us das ich nid gnueg recht han zum das mache :confused:" + } + }, "choose": { "Choice": { "EN": "I Choose **{0}**",