Skip to content

Commit 6decbe6

Browse files
committed
code nitpicks
1 parent f1fac8b commit 6decbe6

File tree

11 files changed

+28
-34
lines changed

11 files changed

+28
-34
lines changed

internal/assets/assets.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,5 @@ func (r *reader) To(target any) error {
4444
}
4545

4646
gz.Close()
47-
4847
return nil
4948
}

internal/dto/response/error.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,25 @@ import (
77

88
type (
99
Error struct {
10-
Message error `json:"message" swaggertype:"string"`
10+
Err error `json:"message" swaggertype:"string"`
1111
}
1212

1313
internalError struct {
1414
Message string `json:"message"`
1515
}
1616
)
1717

18+
var (
19+
InternalServerError = Error{errors.New("internal server error")}
20+
MethodNotAllowed = Error{errors.New("method not allowed")}
21+
NotFound = Error{errors.New("not found")}
22+
)
23+
1824
func (e Error) MarshalJSON() ([]byte, error) {
19-
i := internalError{e.Message.Error()}
25+
i := internalError{e.Err.Error()}
2026
return json.Marshal(i)
2127
}
2228

23-
var (
24-
ErrInternalServerError = errors.New("internal server error")
25-
ErrMethodNotAllowed = errors.New("method not allowed")
26-
ErrNotFound = errors.New("not found")
27-
)
29+
func ErrorOf(err error) Error {
30+
return Error{Err: err}
31+
}

internal/handlers/v1/entry/entry.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import (
2828
func (h *Handler) Entry(ctx *gin.Context) {
2929
entryNo, ok := h.getEntryNo(ctx)
3030
if !ok {
31-
ctx.JSON(http.StatusBadRequest, response.Error{Message: errUnexpectedEntryNo})
31+
ctx.JSON(http.StatusBadRequest, response.ErrorOf(errUnexpectedEntryNo))
3232
return
3333
}
3434

@@ -38,14 +38,14 @@ func (h *Handler) Entry(ctx *gin.Context) {
3838
if err != nil {
3939
switch {
4040
case errors.Is(err, dict.ErrUnexpectedEmptyLemma) || errors.Is(err, dict.ErrUnexpectedEntryNumber):
41-
ctx.JSON(http.StatusBadRequest, response.Error{Message: err})
41+
ctx.JSON(http.StatusBadRequest, response.ErrorOf(err))
4242
case errors.Is(err, dict.ErrLemmaNotFound) || errors.Is(err, dict.ErrEntryNotFound):
43-
ctx.JSON(http.StatusNotFound, response.Error{Message: err})
43+
ctx.JSON(http.StatusNotFound, response.ErrorOf(err))
4444
case errors.Is(err, dict.ErrLemmaTooLong):
45-
ctx.JSON(http.StatusRequestURITooLong, response.Error{Message: err})
45+
ctx.JSON(http.StatusRequestURITooLong, response.ErrorOf(err))
4646
default:
4747
h.logger.ErrorContext(ctx, "Unexpected lemma retrieval failure", slog.String("error", err.Error()))
48-
ctx.JSON(http.StatusInternalServerError, response.Error{Message: response.ErrInternalServerError})
48+
ctx.JSON(http.StatusInternalServerError, response.InternalServerError)
4949
}
5050

5151
return

internal/handlers/v1/entry/random.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package entry
22

33
import (
4-
"log/slog"
54
"net/http"
65

76
"github.com/gin-gonic/gin"
8-
"github.com/raf555/kbbi-api/internal/dto/response"
97
)
108

119
// Entry godoc
@@ -16,12 +14,6 @@ import (
1614
// @Failure 500 {object} response.Error
1715
// @Router /api/v1/entry/_random [get]
1816
func (h *Handler) Random(ctx *gin.Context) {
19-
lemma, err := h.dict.RandomLemma()
20-
if err != nil {
21-
h.logger.ErrorContext(ctx, "Unexpected Random Lemma failure", slog.String("error", err.Error()))
22-
ctx.JSON(http.StatusInternalServerError, response.Error{Message: response.ErrInternalServerError})
23-
return
24-
}
25-
17+
lemma := h.dict.RandomLemma()
2618
ctx.Redirect(http.StatusFound, lemma.Lemma)
2719
}

internal/handlers/v1/entry/wotd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func (h *Handler) WOTD(ctx *gin.Context) {
1919
wotd, err := h.dict.LemmaOfTheDay()
2020
if err != nil {
2121
h.logger.ErrorContext(ctx, "Unexpected WOTD failure", slog.String("error", err.Error()))
22-
ctx.JSON(http.StatusInternalServerError, response.Error{Message: response.ErrInternalServerError})
22+
ctx.JSON(http.StatusInternalServerError, response.InternalServerError)
2323
return
2424
}
2525

internal/logger/slog.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
slogformatter "github.com/samber/slog-formatter"
88
)
99

10-
func NewLogger() *slog.Logger {
10+
func New() *slog.Logger {
1111
timeDurationFormatter := slogformatter.FormatByKind(slog.KindDuration, func(v slog.Value) slog.Value {
1212
return slog.StringValue(v.Duration().String())
1313
})

internal/repositories/dict/dict.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,9 @@ func (d *Dictionary) Lemma(lemma string, entryNoPtr *int) (kbbi.Lemma, error) {
108108
return lemmaData, nil
109109
}
110110

111-
func (d *Dictionary) RandomLemma() (kbbi.Lemma, error) {
111+
func (d *Dictionary) RandomLemma() kbbi.Lemma {
112112
randomIdx := rand.IntN(len(d.lemmas))
113-
114-
return d.lemmas[randomIdx], nil
113+
return d.lemmas[randomIdx]
115114
}
116115

117116
func (d *Dictionary) LemmaOfTheDay() (kbbi.Lemma, error) {

internal/repositories/wotd/wotd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func New(env *config.Configuration) (*Repository, error) {
2929

3030
repo := &Repository{
3131
lemmaIndexes: lemmaIndexes,
32-
epoch: time.Date(2022, 10, 30, 0, 0, 0, 0, loc).UnixMilli(),
32+
epoch: time.Date(2022, time.October, 30, 0, 0, 0, 0, loc).UnixMilli(),
3333
}
3434

3535
return repo, nil

internal/router/router.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ func New(
2929

3030
router.Use(gin.CustomRecovery(func(ctx *gin.Context, err any) {
3131
logger.ErrorContext(ctx, "Panic occured", slog.Any("panic", err))
32-
ctx.JSON(http.StatusInternalServerError, response.Error{Message: response.ErrInternalServerError})
32+
ctx.JSON(http.StatusInternalServerError, response.InternalServerError)
3333
}))
3434

3535
router.NoMethod(func(ctx *gin.Context) {
36-
ctx.JSON(http.StatusMethodNotAllowed, response.Error{Message: response.ErrMethodNotAllowed})
36+
ctx.JSON(http.StatusMethodNotAllowed, response.MethodNotAllowed)
3737
})
3838

3939
router.NoRoute(func(ctx *gin.Context) {
40-
ctx.JSON(http.StatusNotFound, response.Error{Message: response.ErrNotFound})
40+
ctx.JSON(http.StatusNotFound, response.NotFound)
4141
})
4242
}
4343

wire.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ var (
2020
)
2121

2222
mainDepSet = wire.NewSet(
23-
logger.NewLogger,
23+
logger.New,
2424
)
2525

2626
repoSet = wire.NewSet(

0 commit comments

Comments
 (0)