From c1373242bb24eb7e9ee27f639f30205c2c37a16a Mon Sep 17 00:00:00 2001 From: rxdn Date: Sun, 21 Jun 2020 14:57:08 +0100 Subject: [PATCH] error page --- app/http/endpoints/manage/modmaillogsview.go | 35 +++++---------- app/http/endpoints/root/callback.go | 5 ++- app/http/middleware/authenticateguild.go | 15 ++++--- app/http/server.go | 12 +++++ public/static/css/style.css | 9 +++- public/static/js/auth.js | 4 ++ public/templates/includes/navbar.tmpl | 2 +- public/templates/includes/sidebar.tmpl | 2 +- public/templates/layouts/error.tmpl | 47 ++++++++++++++++++++ utils/errorpage.go | 10 +++++ 10 files changed, 105 insertions(+), 36 deletions(-) create mode 100644 public/templates/layouts/error.tmpl create mode 100644 utils/errorpage.go diff --git a/app/http/endpoints/manage/modmaillogsview.go b/app/http/endpoints/manage/modmaillogsview.go index f35a864..0d63733 100644 --- a/app/http/endpoints/manage/modmaillogsview.go +++ b/app/http/endpoints/manage/modmaillogsview.go @@ -3,9 +3,7 @@ package manage import ( "errors" "fmt" - "github.com/TicketsBot/GoPanel/config" "github.com/TicketsBot/GoPanel/database" - "github.com/TicketsBot/GoPanel/rpc/cache" "github.com/TicketsBot/GoPanel/utils" "github.com/TicketsBot/archiverclient" "github.com/TicketsBot/common/permission" @@ -27,50 +25,39 @@ func ModmailLogViewHandler(ctx *gin.Context) { // Verify the guild exists guildId, err := strconv.ParseUint(ctx.Param("id"), 10, 64) if err != nil { - ctx.Redirect(302, config.Conf.Server.BaseUrl) // TODO: 404 Page + utils.ErrorPage(ctx, 404, "Couldn't find a server with the ID provided") return } - // Get object for selected guild - guild, _ := cache.Instance.GetGuild(guildId, false) - // get ticket UUID archiveUuid, err := uuid.FromString(ctx.Param("uuid")) if err != nil { - // TODO: 404 error page - ctx.AbortWithStatusJSON(404, gin.H{ - "success": false, - "error": "Modmail archive not found", - }) + utils.ErrorPage(ctx, 404, "Modmail archive with provided UUID not found") return } // get ticket object archive, err := database.Client.ModmailArchive.Get(archiveUuid) if err != nil { - // TODO: 500 error page - ctx.AbortWithStatusJSON(500, gin.H{ - "success": false, - "error": err.Error(), - }) + utils.ErrorPage(ctx, 500, err.Error()) return } // Verify this is a valid ticket and it is closed - if archive.Uuid == uuid.Nil{ - ctx.Redirect(302, fmt.Sprintf("/manage/%d/logs/modmail", guild.Id)) + if archive.Uuid == uuid.Nil { + utils.ErrorPage(ctx, 404, "Modmail archive with provided UUID not found") return } // Verify this modmail ticket was for this guild if archive.GuildId != guildId { - ctx.Redirect(302, fmt.Sprintf("/manage/%d/logs/modmail", guild.Id)) + utils.ErrorPage(ctx, 403, "Modmail archive does not belong to this server") return } // Verify the user has permissions to be here if utils.GetPermissionLevel(guildId, userId) < permission.Support && archive.UserId != userId { - ctx.Redirect(302, config.Conf.Server.BaseUrl) // TODO: 403 Page + utils.ErrorPage(ctx, 403, "You do not have permission to view this archive") return } @@ -78,18 +65,18 @@ func ModmailLogViewHandler(ctx *gin.Context) { messages, err := Archiver.GetModmail(guildId, archiveUuid.String()) if err != nil { if errors.Is(err, archiverclient.ErrExpired) { - ctx.String(200, "Archives expired: Purchase premium for permanent log storage") // TODO: Actual error page - return + utils.ErrorPage(ctx, 404, "Archive expired - purchase premium for permanent log storage") + } else { + utils.ErrorPage(ctx, 500, err.Error()) } - ctx.String(500, fmt.Sprintf("Failed to retrieve archive - please contact the developers: %s", err.Error())) return } // format to html html, err := Archiver.Encode(messages, fmt.Sprintf("modmail-%s", archiveUuid)) if err != nil { - ctx.String(500, fmt.Sprintf("Failed to retrieve archive - please contact the developers: %s", err.Error())) + utils.ErrorPage(ctx, 500, err.Error()) return } diff --git a/app/http/endpoints/root/callback.go b/app/http/endpoints/root/callback.go index 98c5a13..dfc7f2a 100644 --- a/app/http/endpoints/root/callback.go +++ b/app/http/endpoints/root/callback.go @@ -50,13 +50,14 @@ func CallbackHandler(ctx *gin.Context) { code, ok := ctx.GetQuery("code") if !ok { - ctx.String(400, "Discord provided an invalid Oauth2 code") + utils.ErrorPage(ctx, 400, "Discord provided invalid Oauth2 code") return } res, err := discord.AccessToken(code) if err != nil { - ctx.String(500, err.Error()) + utils.ErrorPage(ctx, 500, err.Error()) + return } store.Set("access_token", res.AccessToken) diff --git a/app/http/middleware/authenticateguild.go b/app/http/middleware/authenticateguild.go index d891179..56879b1 100644 --- a/app/http/middleware/authenticateguild.go +++ b/app/http/middleware/authenticateguild.go @@ -17,14 +17,15 @@ func AuthenticateGuild(isApiMethod bool) gin.HandlerFunc { parsed, err := strconv.ParseUint(guildId, 10, 64) if err != nil { if isApiMethod { - ctx.AbortWithStatusJSON(400, gin.H{ + ctx.JSON(400, gin.H{ "success": false, "error": "Invalid guild ID", }) } else { - ctx.Redirect(302, config.Conf.Server.BaseUrl) // TODO: 404 Page + utils.ErrorPage(ctx, 400, "Invalid server ID") ctx.Abort() } + ctx.Abort() return } @@ -35,7 +36,7 @@ func AuthenticateGuild(isApiMethod bool) gin.HandlerFunc { if isApiMethod { ctx.Redirect(302, fmt.Sprintf("https://invite.ticketsbot.net/?guild_id=%d&disable_guild_select=true&response_type=code&scope=bot%%20identify&redirect_uri=%s", parsed, config.Conf.Server.BaseUrl)) } else { - ctx.Redirect(302, config.Conf.Server.BaseUrl) // TODO: 404 Page + utils.ErrorPage(ctx, 404, "Couldn't find server with the provided ID") } ctx.Abort() return @@ -52,20 +53,20 @@ func AuthenticateGuild(isApiMethod bool) gin.HandlerFunc { "error": "Unauthorized", }) } else { - ctx.Redirect(302, config.Conf.Server.BaseUrl) // TODO: 403 Page + utils.ErrorPage(ctx, 403, "You don't have permission to be here - make sure the server owner has added you as an admin with t!addadmin") ctx.Abort() } } } else { if isApiMethod { - ctx.AbortWithStatusJSON(400, gin.H{ + ctx.JSON(400, gin.H{ "success": false, "error": "Invalid guild ID", }) } else { - ctx.Redirect(302, config.Conf.Server.BaseUrl) // TODO: 404 Page - ctx.Abort() + utils.ErrorPage(ctx, 400, "Invalid server ID") } + ctx.Abort() } } } diff --git a/app/http/server.go b/app/http/server.go index 4081ed0..a4cec81 100644 --- a/app/http/server.go +++ b/app/http/server.go @@ -149,6 +149,8 @@ func createRenderer() multitemplate.Renderer { r = addManageTemplate(r, "panels", "./public/templates/includes/substitutionmodal.tmpl", "./public/templates/includes/paneleditmodal.tmpl") r = addManageTemplate(r, "tags") + r = addErrorTemplate(r) + return r } @@ -183,6 +185,16 @@ func addManageTemplate(renderer multitemplate.Renderer, name string, extra ...st return renderer } +func addErrorTemplate(renderer multitemplate.Renderer) multitemplate.Renderer { + files := []string{ + "./public/templates/layouts/error.tmpl", + "./public/templates/includes/head.tmpl", + } + + renderer.AddFromFiles("error", files...) + return renderer +} + func createLimiter(limit int64, period time.Duration) func(*gin.Context) { store := memory.NewStore() rate := limiter.Rate{ diff --git a/public/static/css/style.css b/public/static/css/style.css index 2af8984..8d699fa 100644 --- a/public/static/css/style.css +++ b/public/static/css/style.css @@ -1,5 +1,12 @@ +@import url('https://fonts.googleapis.com/css2?family=Noto+Sans&display=swap'); + +body, h1, .h1, h2, .h2, h3, .h3, h4, .h4, h5, .h5, h6, .h6, p, .navbar, .brand, .btn-simple, .alert, a, .td-name, td, button.close { + font-family: 'Noto Sans', sans-serif !important; + + font-weight: 400 !important; +} + body { - font-family: 'Open Sans',sans-serif !important; font-size: 1rem; font-weight: 400; line-height: 1.5; diff --git a/public/static/js/auth.js b/public/static/js/auth.js index 358c15d..a9b20e7 100644 --- a/public/static/js/auth.js +++ b/public/static/js/auth.js @@ -18,6 +18,10 @@ async function getToken() { return token; } +function clearLocalStorage() { + window.localStorage.clear(); +} + async function setDefaultHeader() { const token = await getToken(); axios.defaults.headers.common['Authorization'] = token; diff --git a/public/templates/includes/navbar.tmpl b/public/templates/includes/navbar.tmpl index 7eea7c0..24d2455 100644 --- a/public/templates/includes/navbar.tmpl +++ b/public/templates/includes/navbar.tmpl @@ -28,7 +28,7 @@ Servers