Skip to content

Commit 186ba71

Browse files
Merge pull request #16 from nicholas-fedor/15-no-error-checks-for-fmtsscanf-and-templrender-methods
15 no error checks for fmtsscanf and templrender methods
2 parents 48868cf + e542c58 commit 186ba71

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

internal/eui64/eui64.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@ func CalculateEUI64(macStr, prefixStr string) (string, string, error) {
6464
ip6 := make([]uint16, 8)
6565
for i := 0; i < 8; i++ {
6666
if i < len(prefixParts) {
67-
fmt.Sscanf(prefixParts[i], "%x", &ip6[i])
67+
_, err := fmt.Sscanf(prefixParts[i], "%x", &ip6[i])
68+
if err != nil {
69+
return "", "", fmt.Errorf("failed to parse hextet: %v", err)
70+
}
6871
} else if i < 4 {
6972
ip6[i] = 0 // Fill remaining prefix hextets with zeros
7073
} else {

internal/handlers/handlers.go

+17-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package handlers
22

33
import (
4+
"log"
5+
"net/http"
6+
47
"github.com/gin-gonic/gin"
58
"github.com/nicholas-fedor/EUI64-Calculator/internal/eui64"
69
"github.com/nicholas-fedor/EUI64-Calculator/ui"
@@ -14,7 +17,13 @@ func NewHandler() *Handler {
1417
}
1518

1619
func (h *Handler) Home(c *gin.Context) {
17-
ui.Home().Render(c.Request.Context(), c.Writer)
20+
if err := ui.Home().Render(c.Request.Context(), c.Writer); err != nil {
21+
if err := c.AbortWithError(http.StatusInternalServerError, err); err != nil {
22+
// Log the error since there's nothing more we can do here
23+
log.Printf("Failed to abort with error: %v", err)
24+
}
25+
return
26+
}
1827
}
1928

2029
func (h *Handler) Calculate(c *gin.Context) {
@@ -31,5 +40,11 @@ func (h *Handler) Calculate(c *gin.Context) {
3140
data.Error = err.Error()
3241
}
3342

34-
ui.Result(data).Render(c.Request.Context(), c.Writer)
43+
if err := ui.Result(data).Render(c.Request.Context(), c.Writer); err != nil {
44+
if err := c.AbortWithError(http.StatusInternalServerError, err); err != nil {
45+
// Log the error for debugging purposes
46+
log.Printf("Failed to abort with error: %v", err)
47+
}
48+
return
49+
}
3550
}

0 commit comments

Comments
 (0)