Skip to content

Commit

Permalink
chore: add debug log
Browse files Browse the repository at this point in the history
  • Loading branch information
saitofun committed Jun 21, 2024
1 parent 08d1ceb commit 3ec0b74
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
22 changes: 18 additions & 4 deletions pkg/modules/event/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,31 @@ func WrapHandleError(e error, t any) error {
if e == nil {
return nil
}
return &HandleError{t, e}
return &HandleError{t, e, ""}
}

func WrapHandleErrorf(e error, t any, msg string, args ...any) error {
if e == nil {
return nil
}
if len(args) > 0 && msg != "" {
msg = fmt.Sprintf(msg, args...)
}
return &HandleError{t, e, msg}
}

type HandleError struct {
t any
e error
t any
e error
msg string
}

func (e *HandleError) Error() string {
return fmt.Sprintf(
msg := fmt.Sprintf(
"failed to handle event `%s`: %s",
reflect.Indirect(reflect.ValueOf(e.t)).Type(), e.e.Error(),
)
if e.msg != "" {
return msg + " " + e.msg
}
}

Check failure on line 87 in pkg/modules/event/errors.go

View workflow job for this annotation

GitHub Actions / build

missing return

Check failure on line 87 in pkg/modules/event/errors.go

View workflow job for this annotation

GitHub Actions / build

missing return
17 changes: 13 additions & 4 deletions pkg/modules/event/event_device_confirm.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,16 @@ type message struct {
}

func (e *DeviceConfirm) Handle(ctx context.Context) (err error) {
defer func() { err = WrapHandleError(err, e) }()
projectID := must.BeTrueV(contexts.ProjectIDFromContext(ctx))
projectVersion := must.BeTrueV(contexts.ProjectVersionFromContext(ctx))

defer func() {
err = WrapHandleErrorf(
err, e,
"project_id: %d project_version: %s",
projectID, projectVersion,
)
}()

dev := &models.Device{ID: e.imei}
err = FetchByPrimary(ctx, dev)
Expand All @@ -104,8 +113,8 @@ func (e *DeviceConfirm) Handle(ctx context.Context) (err error) {
msg := &models.Message{
MessageID: dev.Address + fmt.Sprintf("-%d", e.pkg.GetTimestamp()),
ClientID: dev.Address,
ProjectID: must.BeTrueV(contexts.ProjectIDFromContext(ctx)),
ProjectVersion: must.BeTrueV(contexts.ProjectVersionFromContext(ctx)),
ProjectID: projectID,
ProjectVersion: projectVersion,
Data: must.NoErrorV(json.Marshal([]message{{
IMEI: e.imei,
Owner: dev.Owner,
Expand All @@ -117,7 +126,7 @@ func (e *DeviceConfirm) Handle(ctx context.Context) (err error) {
InternalTaskID: id,
}
task := &models.Task{
ProjectID: must.BeTrueV(contexts.ProjectIDFromContext(ctx)),
ProjectID: projectID,
InternalTaskID: id,
MessageIDs: datatypes.JSON([]byte(`["` + id + `"]`)),
Signature: "",
Expand Down

0 comments on commit 3ec0b74

Please sign in to comment.