From 4bce28d677210c1fbf6a7554a4a400d52087e770 Mon Sep 17 00:00:00 2001 From: Luqqk Date: Sun, 1 Aug 2021 16:24:45 +0200 Subject: [PATCH] client.Enqueue - prevent empty task's typename --- client.go | 6 ++++++ client_test.go | 5 +++++ 2 files changed, 11 insertions(+) diff --git a/client.go b/client.go index 768135c..3a5de24 100644 --- a/client.go +++ b/client.go @@ -178,6 +178,9 @@ func (d processInOption) Value() interface{} { return time.Duration(d) } // ErrDuplicateTask error only applies to tasks enqueued with a Unique option. var ErrDuplicateTask = errors.New("task already exists") +// ErrEmptyTypeTask indicates that task's typename is not specified. +var ErrEmptyTypeTask = errors.New("task typename not specified") + type option struct { retry int queue string @@ -266,6 +269,9 @@ func (c *Client) Close() error { // // If no ProcessAt or ProcessIn options are provided, the task will be pending immediately. func (c *Client) Enqueue(task *Task, opts ...Option) (*TaskInfo, error) { + if task.Type() == "" { + return nil, fmt.Errorf("%w", ErrEmptyTypeTask) + } c.mu.Lock() if defaults, ok := c.opts[task.Type()]; ok { opts = append(defaults, opts...) diff --git a/client_test.go b/client_test.go index 5a50a2f..3517af1 100644 --- a/client_test.go +++ b/client_test.go @@ -585,6 +585,11 @@ func TestClientEnqueueError(t *testing.T) { Queue(""), }, }, + { + desc: "With empty task typename", + task: NewTask("", h.JSON(map[string]interface{}{})), + opts: []Option{}, + }, } for _, tc := range tests {