Skip to content

Commit

Permalink
refactor(presentation): change type assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
bastean committed Sep 5, 2024
1 parent 2198845 commit b1d4e18
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 7 deletions.
3 changes: 2 additions & 1 deletion internal/app/server/handler/page/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package page
import (
"github.com/bastean/codexgo/v4/internal/app/server/component/page/dashboard"
"github.com/bastean/codexgo/v4/internal/app/server/util/errs"
"github.com/bastean/codexgo/v4/internal/app/server/util/format"
"github.com/bastean/codexgo/v4/internal/app/server/util/key"
"github.com/bastean/codexgo/v4/internal/pkg/service/errors"
"github.com/bastean/codexgo/v4/internal/pkg/service/module/user"
Expand All @@ -19,7 +20,7 @@ func Dashboard(c *gin.Context) {

query := new(user.ReadQuery)

query.Id = id.(string)
query.Id = format.ToString(id)

found, err := user.Read.Handle(query)

Expand Down
3 changes: 2 additions & 1 deletion internal/app/server/handler/user/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net/http"

"github.com/bastean/codexgo/v4/internal/app/server/util/errs"
"github.com/bastean/codexgo/v4/internal/app/server/util/format"
"github.com/bastean/codexgo/v4/internal/app/server/util/key"
"github.com/bastean/codexgo/v4/internal/app/server/util/reply"
"github.com/bastean/codexgo/v4/internal/pkg/service/errors"
Expand All @@ -29,7 +30,7 @@ func Delete(c *gin.Context) {
return
}

command.Id = id.(string)
command.Id = format.ToString(id)

err = user.Delete.Handle(command)

Expand Down
3 changes: 2 additions & 1 deletion internal/app/server/handler/user/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net/http"

"github.com/bastean/codexgo/v4/internal/app/server/util/errs"
"github.com/bastean/codexgo/v4/internal/app/server/util/format"
"github.com/bastean/codexgo/v4/internal/app/server/util/key"
"github.com/bastean/codexgo/v4/internal/app/server/util/reply"
"github.com/bastean/codexgo/v4/internal/pkg/service/errors"
Expand All @@ -28,7 +29,7 @@ func Update(c *gin.Context) {
return
}

command.Id = id.(string)
command.Id = format.ToString(id)

err = user.Update.Handle(command)

Expand Down
3 changes: 2 additions & 1 deletion internal/app/server/middleware/authentication.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"strings"

"github.com/bastean/codexgo/v4/internal/app/server/util/errs"
"github.com/bastean/codexgo/v4/internal/app/server/util/format"
"github.com/bastean/codexgo/v4/internal/app/server/util/key"
"github.com/bastean/codexgo/v4/internal/pkg/service/authentication/jwt"
"github.com/bastean/codexgo/v4/internal/pkg/service/errors"
Expand All @@ -21,7 +22,7 @@ func Authentication(c *gin.Context) {
return
}

signature := strings.Split(token.(string), " ")[1]
signature := strings.Split(format.ToString(token), " ")[1]

claims, err := jwt.Validate(signature)

Expand Down
8 changes: 8 additions & 0 deletions internal/app/server/middleware/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ func ErrorHandler() gin.HandlerFunc {
route, ok := route.(string)

if !ok {
log.Error(errors.NewInternal(&errors.Bubble{
Where: "ErrorHandler",
What: "Invalid redirection Route",
Why: errors.Meta{
"Route": route,
},
}).Error())

route = "/"
}

Expand Down
16 changes: 15 additions & 1 deletion internal/app/server/middleware/recover.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,27 @@ import (
"net/http"

"github.com/bastean/codexgo/v4/internal/app/server/util/reply"
"github.com/bastean/codexgo/v4/internal/pkg/service/errors"
"github.com/bastean/codexgo/v4/internal/pkg/service/logger/log"
"github.com/gin-gonic/gin"
)

func Recover() gin.RecoveryFunc {
return func(c *gin.Context, err any) {
log.Error(err.(error).Error())
failure, ok := err.(error)

if !ok {
failure = errors.NewInternal(&errors.Bubble{
Where: "Recover",
What: "Unknown Error",
Why: errors.Meta{
"Error": err,
},
})
}

log.Error(failure.Error())

c.AbortWithStatusJSON(http.StatusInternalServerError, &reply.JSON{Message: "Server error. Try again later."})
}
}
9 changes: 9 additions & 0 deletions internal/app/server/util/format/format.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package format

import (
"fmt"
)

func ToString(value any) string {
return fmt.Sprintf("%v", value)
}
4 changes: 2 additions & 2 deletions internal/pkg/service/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (

type (
key = string
what = any
required = bool
what = any
)

const (
Expand Down Expand Up @@ -97,7 +97,7 @@ func Verify() error {

func Init() error {
if err := Verify(); err != nil {
return errors.BubbleUp(err, "Up")
return errors.BubbleUp(err, "Init")
}

SMTP()
Expand Down
1 change: 1 addition & 0 deletions internal/pkg/service/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

type (
Bubble = errors.Bubble
Meta = errors.Meta
)

type (
Expand Down

0 comments on commit b1d4e18

Please sign in to comment.