Skip to content

Commit 8ea250a

Browse files
committed
chore: fix codes
1 parent 6e5a8fb commit 8ea250a

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

pbtask.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package scheduler
33
import (
44
"bytes"
55
"context"
6+
"errors"
67
"fmt"
78
"io"
89
"net/http"
@@ -12,6 +13,8 @@ import (
1213
"google.golang.org/protobuf/types/known/timestamppb"
1314
)
1415

16+
var ErrInvalidTask = errors.New("invalid task")
17+
1518
func PbTaskToTask(ctx context.Context, queuePath, taskIDPrefix string, pb *taskspb.Task) (*Task, error) {
1619
id, version, err := ParseTaskName(taskIDPrefix, pb.Name)
1720
if err != nil {
@@ -34,7 +37,7 @@ func PbTaskToTask(ctx context.Context, queuePath, taskIDPrefix string, pb *tasks
3437
}
3538
authorizationToken = at
3639
default:
37-
return nil, fmt.Errorf("unsupported message type: %s", pb.MessageType)
40+
return nil, fmt.Errorf("unsupported message type (%s): %w", pb.MessageType, ErrInvalidTask)
3841
}
3942

4043
return &Task{
@@ -66,7 +69,7 @@ func convertHTTPRequest(ctx context.Context, req *taskspb.HttpRequest) (*http.Re
6669
case taskspb.HttpMethod_OPTIONS:
6770
method = http.MethodOptions
6871
default:
69-
return nil, fmt.Errorf("unsupported http method: %s", req.HttpMethod.String())
72+
return nil, fmt.Errorf("unsupported http method (%s): %w", req.HttpMethod.String(), ErrInvalidTask)
7073
}
7174

7275
var body io.Reader
@@ -104,7 +107,7 @@ func convertAuthorizationToken(req *taskspb.HttpRequest) (isAuthorizationToken,
104107
}, nil
105108

106109
default:
107-
return nil, fmt.Errorf("unsupported authorization header type: %s", h)
110+
return nil, fmt.Errorf("unsupported authorization header type (%+v): %w", h, ErrInvalidTask)
108111
}
109112
}
110113

scheduler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ func (s *Scheduler) Create(ctx context.Context, task *Task, opts ...gax.CallOpti
130130
case codes.AlreadyExists:
131131
return ErrTaskAlreadyExists
132132
default:
133-
return err
133+
return fmt.Errorf("failed to create task: %w", err)
134134
}
135135
}
136136

@@ -142,7 +142,7 @@ func (s *Scheduler) Delete(ctx context.Context, taskName string, opts ...gax.Cal
142142
Name: taskName,
143143
}
144144
if err := s.client.DeleteTask(ctx, req, opts...); err != nil {
145-
return err
145+
return fmt.Errorf("failed to delete task: %w", err)
146146
}
147147

148148
return nil

task.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ const (
1616
taskVersionSeparator = "v"
1717
)
1818

19+
var ErrInvalidTaskName = errors.New("invalid task name")
20+
1921
type Task struct {
2022
QueuePath string
2123
Prefix string
@@ -125,25 +127,26 @@ func ParseTaskName(prefix, taskName string) (string, int, error) {
125127
v := path.Base(taskName)
126128

127129
if !strings.HasPrefix(v, prefix) {
128-
return "", 0, errors.New("task name has no valid prefix")
130+
return "", 0, fmt.Errorf("task name has no valid prefix: %w", ErrInvalidTaskName)
129131
}
130132

131133
v = strings.TrimPrefix(v, prefix)
132134
tsIdx := strings.LastIndex(v, taskTimestampSeparator)
133135
if tsIdx < 0 {
134-
return "", 0, errors.New("invalid task name format")
136+
return "", 0, fmt.Errorf("invalid task name format: %w", ErrInvalidTaskName)
135137
}
136138

137139
id := v[:tsIdx]
138140
v = v[tsIdx+1:]
139141
vIdx := strings.LastIndex(v, taskVersionSeparator)
140142
if vIdx < 0 {
141-
return "", 0, errors.New("task name has no valid version")
143+
return "", 0, fmt.Errorf("task name has no valid version: %w", ErrInvalidTaskName)
142144
}
143145

144-
version, err := strconv.Atoi(v[vIdx+1:])
146+
vs := v[vIdx+1:]
147+
version, err := strconv.Atoi(vs)
145148
if err != nil {
146-
return "", 0, fmt.Errorf("failed to parse version: %w", err)
149+
return "", 0, fmt.Errorf("failed to parse version (%s): %w", vs, ErrInvalidTaskName)
147150
}
148151

149152
return id, version, nil

0 commit comments

Comments
 (0)