feat: error handling for homepage

This commit is contained in:
httpjamesm 2022-12-27 23:42:54 -05:00
parent 2ae093b43d
commit 9071bc23db
3 changed files with 30 additions and 3 deletions

View File

@ -36,7 +36,9 @@ func main() {
body := urlConversionRequest{}
if err := c.ShouldBind(&body); err != nil {
c.JSON(400, gin.H{"success": false, "message": "Invalid request body"})
c.HTML(400, "home.html", gin.H{
"errorMessage": "Invalid request body",
})
return
}
@ -44,7 +46,9 @@ func main() {
// validate URL
if !strings.HasPrefix(soLink, "https://stackoverflow.com/questions/") {
c.JSON(400, gin.H{"success": false, "message": "Invalid URL"})
c.HTML(400, "home.html", gin.H{
"errorMessage": "Invalid stack overflow URL",
})
return
}

View File

@ -66,6 +66,19 @@ body {
background-color: #3b404b;
}
.error {
width: 100%;
padding: .5rem;
background-color: rgb(255, 129, 129);
color: black;
border-radius: 5px;
margin-bottom: 1rem;
}
.error p {
margin: 0;
}
@media screen and (max-width: 800px) {
body {
padding: 1rem;

View File

@ -16,9 +16,19 @@
browsing habits and other browser fingerprint data to
StackOverflow.
</p>
{{ if .errorMessage }}
<div class="error">
<p><b>Error</b>: {{ .errorMessage }}</p>
</div>
{{ end }}
<form method="POST">
<div class="view-form">
<input class="view-input" type="text" name="url" placeholder="https://stackoverflow.com/questions/123456/example-url" />
<input
class="view-input"
type="text"
name="url"
placeholder="https://stackoverflow.com/questions/123456/example-url"
/>
<button class="view-button" type="submit">View</button>
</div>
</form>