Skip to content

Commit fd9dc6c

Browse files
0xmadmichiosw
authored andcommitted
feat(web): add error page
1 parent 1abb3a1 commit fd9dc6c

File tree

3 files changed

+78
-5
lines changed

3 files changed

+78
-5
lines changed

internal/web/handlers/ui.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ func NewUIHandler(logger log.Logger) http.Handler {
2323
mux.HandleFunc("/", h.serveIndex)
2424
mux.HandleFunc("/add", h.serveAddPage)
2525
mux.HandleFunc("/success", h.serveSuccessPage)
26+
mux.HandleFunc("/error", h.serveErrorPage)
2627

2728
return mux
2829
}
@@ -32,17 +33,17 @@ func (h *UIHandler) serveIndex(w http.ResponseWriter, r *http.Request) {
3233
err := templates.IndexPage(templates.IndexPageProps{
3334
Servers: []domain.ServerItem{
3435
{
35-
ID: "1",
36+
ID: "1",
3637
Name: "Server Alpha",
3738
IP: "192.168.1.10",
3839
},
3940
{
40-
ID: "2",
41+
ID: "2",
4142
Name: "Server Beta",
4243
IP: "192.168.1.11",
4344
},
4445
{
45-
ID: "3",
46+
ID: "3",
4647
Name: "Server Charlie",
4748
IP: "192.168.1.12",
4849
},
@@ -83,3 +84,20 @@ func (h *UIHandler) serveSuccessPage(w http.ResponseWriter, r *http.Request) {
8384
http.Error(w, "failed to render page", http.StatusInternalServerError)
8485
}
8586
}
87+
88+
func (h *UIHandler) serveErrorPage(w http.ResponseWriter, r *http.Request) {
89+
errorText := r.URL.Query().Get("error")
90+
91+
var text string
92+
93+
if errorText != "" {
94+
text = fmt.Sprintf("Operation failed. Reason: '%s'", errorText)
95+
}
96+
97+
err := templates.ErrorPage(templates.ErrorPageProps{Text: text}).Render(r.Context(), w)
98+
99+
if err != nil {
100+
h.log.Error().Err(err).Msg("failed to render error page template")
101+
http.Error(w, "failed to render page", http.StatusInternalServerError)
102+
}
103+
}

internal/web/templates/error.templ

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package templates
2+
3+
import (
4+
"github.com/co-browser/agent-browser/internal/web/templates/blocks"
5+
"github.com/co-browser/agent-browser/internal/web/templates/components"
6+
)
7+
8+
type ErrorPageProps struct {
9+
Text string
10+
}
11+
12+
css errorContainer() {
13+
padding: 20px 16px;
14+
display: flex;
15+
flex-direction: column;
16+
align-items: center;
17+
justify-content: center;
18+
}
19+
20+
css errorGoBackLink() {
21+
display: flex;
22+
align-items: center;
23+
justify-content: center;
24+
height: 40px;
25+
background-color: #ff4d4d;
26+
color: white;
27+
border: none;
28+
border-radius: 4px;
29+
}
30+
31+
css errorText() {
32+
text-align: center;
33+
}
34+
35+
templ ErrorPageComponent(props ErrorPageProps) {
36+
<section class={ errorContainer() } data-testid="error-page">
37+
<h2>Error</h2>
38+
<div>
39+
if props.Text != "" {
40+
<p class={ errorText() }>{ props.Text }</p>
41+
} else {
42+
<p class={ errorText() }>Operation failed!</p>
43+
}
44+
@components.Link(components.LinkProps{Name: "Go back to home", Href: "/", Class: errorGoBackLink()})
45+
</div>
46+
</section>
47+
}
48+
49+
templ ErrorPage(props ErrorPageProps) {
50+
@blocks.Main(ErrorPageComponent(props))
51+
}

internal/web/templates/success.templ

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,18 @@ css successGoBackLink() {
2828
border-radius: 4px;
2929
}
3030

31+
css successText() {
32+
text-align: center;
33+
}
34+
3135
templ SuccessPageComponent(props SuccessPageProps) {
3236
<section class={ successContainer() } data-testid="success-page">
3337
<h2>Success</h2>
3438
<div>
3539
if props.Text != "" {
36-
<p>{ props.Text }</p>
40+
<p class={ successText() }>{ props.Text }</p>
3741
} else {
38-
<p>Operation completed successfully!</p>
42+
<p class={ successText() }>Operation completed successfully!</p>
3943
}
4044
@components.Link(components.LinkProps{Name: "Go back to home", Href: "/", Class: successGoBackLink()})
4145
</div>

0 commit comments

Comments
 (0)