capnspacehook / taskmaster

Windows Task Scheduler Library for Go
MIT License
142 stars 29 forks source link

DeleteTask should wrap error to detect IsNotExist errors #26

Open maja42 opened 3 years ago

maja42 commented 3 years ago

It's currently not possible to check if a task actually exists. I'd like to delete a task, and if it does not exist I want to ignore the error. Usually this can be done by checking os.IsNotExist(err). However, taskService.DeleteTask wraps the underlying OS error with the verb %v, which effectively loses type information:

return fmt.Errorf("error deleting task %s: %v", path, getTaskSchedulerError(err))

Taskmaster should use %w instead.

I could also try something with GetRegisteredTask, but the goDoc doesn't really make sense:

// GetRegisteredTask attempts to find the specified registered task and returns a // pointer to it if it exists. If it doesn't exist, nil will be returned in place of // the registered task.

The return type is a struct, it can't be nil. I also can't compare it with the struct's zero-value, because the struct is not comparable due to the embedded *ole.IDispatch. But that doesn't matter, because GetRegisteredTask also returns an error - the same one as DeleteTask.

Right now, the only way I can check for this scenario is by comparing error messages - which is pretty fragile, considering that windows likes to translate error messages.

capnspacehook commented 3 years ago

I originally wrote taskmaster right around when Go 1.13 was released, so I wasn't familiar with the new errors api at the time. I agree that errors should be wrapped correctly though.