Skip to content

Commit fd49460

Browse files
authored
feat(error): add server error handling (#990)
- Implement server error route - Add server error handler - Update error messages in locales - Create server error component in frontend - Fix formatting in Unauthorized.vue
1 parent c04393b commit fd49460

File tree

10 files changed

+62
-8
lines changed

10 files changed

+62
-8
lines changed
Lines changed: 3 additions & 0 deletions
Loading
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<template>
2+
<div
3+
class="error_page_bg h-[calc(100vh-80px)] min-h-[500px] flex items-center justify-center m-auto">
4+
<div class="flex flex-col items-center">
5+
<div
6+
class="flex flex-col gap-[24px] items-center text-center mt-[24px] px-[16px] text-gray-700">
7+
<div
8+
class="rounded-lg border border-gray-200 bg-white shadow-xs p-[14px]">
9+
<svg-icon name="alert"></svg-icon>
10+
</div>
11+
<p class="font-semibold text-6xl leading-[72px] sm:text-4xl sm:leading-[36px]">
12+
{{ $t('errors_page.serverError.title', { errorCode }) }}
13+
</p>
14+
<p class="text-xl font-[300]">
15+
{{ errorMessage }}
16+
</p>
17+
18+
<!-- action button -->
19+
<div class="flex gap-[12px] mt-[24px]">
20+
<a href="/">
21+
<button
22+
class="text-lg flex px-[22px] py-[16px] items-center justify-center gap-[8px] rounded-sm border border-gray-400 bg-white shadow-sm">
23+
{{ $t('errors_page.login_failed.back_to_homepage') }}
24+
</button>
25+
</a>
26+
</div>
27+
</div>
28+
</div>
29+
</div>
30+
</template>
31+
32+
<script setup>
33+
const url = new URL(window.location.href)
34+
const params = new URLSearchParams(url.search)
35+
const errorCode = params.get('error_code')
36+
const errorMessage = params.get('error_message')
37+
</script>

frontend/src/components/error/Unauthorized.vue

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,19 @@
1212

1313
<h1 id="error-Header" class="text-6xl font-medium mb-6 text-gray-900">
1414
{{ $t('errors_page.unauthorized.title') }}
15-
</h1>
15+
</h1>
1616

1717
<p id="error-Description" class="text-xl font-light mb-12 text-gray-600">
1818
{{ $t('errors_page.unauthorized.desc') }}
1919
</p>
2020

2121
<div id="error-button" class="flex gap-5">
22-
<a href="javascript:history.back();"
22+
<a href="javascript:history.back();"
2323
class="btn btn-secondary bg-white px-6 py-4 font-medium text-gray-700 border border-gray-300 hover:bg-gray-50">
2424
{{ $t('errors_page.not_found.back') }}
2525
</a>
2626

27-
<a href="/"
27+
<a href="/"
2828
class="btn btn-primary bg-brand-600 px-6 py-4 font-medium text-white hover:bg-brand-700">
2929
{{ $t('errors_page.not_found.home') }}
3030
</a>
@@ -34,7 +34,3 @@
3434
</div>
3535
</div>
3636
</template>
37-
38-
<script setup>
39-
40-
</script>

frontend/src/locales/en_js/error.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export const errors_page = {
33
title: 'Page not found',
44
desc: 'Sorry, OpenCSG could not find this web page. This web page may have been removed due to a product update, or your URL is incorrect.',
55
back: 'Back',
6-
home: 'To homepage',
6+
home: 'To homepage',
77
},
88
unauthorized: {
99
title: 'Unauthorized',
@@ -15,4 +15,7 @@ export const errors_page = {
1515
back_to_homepage: 'To homepage',
1616
relogin: 'Relogin',
1717
},
18+
serverError: {
19+
title: 'Server Error {errorCode}'
20+
}
1821
}

frontend/src/locales/zh_js/error.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,7 @@ export const errors_page = {
1515
back_to_homepage: '回到首页',
1616
relogin: '重新登录',
1717
},
18+
serverError: {
19+
title: '服务器错误 {errorCode}'
20+
}
1821
}

frontend/src/main.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import CollectionsDetail from "./components/collections/CollectionsDetail.vue"
5151
import NotFound from "./components/error/NotFound.vue"
5252
import Unauthorized from "./components/error/Unauthorized.vue"
5353
import LoginFailed from "./components/error/LoginFailed.vue"
54+
import ServerError from "./components/error/ServerError.vue"
5455
import FlashMessage from "./components/shared/FlashMessage.vue"
5556
import ResourceConsoleIndex from "./components/resource_console/ResourceConsoleIndex.vue"
5657
import PromptsHub from "./components/prompts/PromptsHub.vue"
@@ -108,6 +109,7 @@ const app = createApp({
108109
NotFound,
109110
Unauthorized,
110111
LoginFailed,
112+
ServerError,
111113
FlashMessage,
112114
ResourceConsoleIndex,
113115
PromptsHub,
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{{ define "content" }}
2+
<server-error />
3+
{{ end }}

internal/handlers/render/error.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ type ErrorHandler interface {
88
NotFound(ctx *gin.Context)
99
Unauthorized(ctx *gin.Context)
1010
LoginFailed(ctx *gin.Context)
11+
ServerError(ctx *gin.Context)
1112
}
1213

1314
type ErrorHandlerImpl struct {
@@ -28,3 +29,7 @@ func (e *ErrorHandlerImpl) Unauthorized(ctx *gin.Context) {
2829
func (e *ErrorHandlerImpl) LoginFailed(ctx *gin.Context) {
2930
RenderBaseInstance.RenderTemplate(ctx, "errors_login_failed", nil)
3031
}
32+
33+
func (e *ErrorHandlerImpl) ServerError(ctx *gin.Context) {
34+
RenderBaseInstance.RenderTemplate(ctx, "errors_server_error", nil)
35+
}

internal/routes/error.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ func registerErrorRoutes(engine *gin.Engine, handlersRegistry *HandlersRegistry)
88
engine.GET("/errors/not-found", handlersRegistry.RenderHandler.ErrorHandler.NotFound)
99
engine.GET("/errors/unauthorized", handlersRegistry.RenderHandler.ErrorHandler.Unauthorized)
1010
engine.GET("/errors/login-failed", handlersRegistry.RenderHandler.ErrorHandler.LoginFailed)
11+
engine.GET("/errors/server-error", handlersRegistry.RenderHandler.ErrorHandler.ServerError)
1112
}

internal/routes/router.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ func createRender() multitemplate.Renderer {
121121
"errors_404": "errors/404.html",
122122
"errors_401": "errors/unauthorized.html",
123123
"errors_login_failed": "errors/login_failed.html",
124+
"errors_server_error": "errors/server_error.html",
124125

125126
"index": "home/index.html",
126127
"models_index": "models/index.html",

0 commit comments

Comments
 (0)