Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] - Panic on malformed time is inscrutable. #55

Closed
flowchartsman opened this issue Sep 3, 2020 · 1 comment · Fixed by #56
Closed

[BUG] - Panic on malformed time is inscrutable. #55

flowchartsman opened this issue Sep 3, 2020 · 1 comment · Fixed by #56
Labels
bug Something isn't working

Comments

@flowchartsman
Copy link

flowchartsman commented Sep 3, 2020

Describe the bug

The library panics if a time is malformed. Specifically in this case if the user forgets a preceeding zero, which I feel is probably a relatively easy mistake to make. I beat my head against the wall for an hour or so on this one before intuition lead me to try putting a zero in front of it.

To Reproduce

Minimal pathological example based on docs.

package main

import (
        "fmt"
        "time"

        "github.com/go-co-op/gocron"
)

func task() {
        fmt.Println("I am running task.")
}

func taskWithParams(a int, b string) {
        fmt.Println(a, b)
}

func main() {
        // defines a new scheduler that schedules and runs jobs
        s1 := gocron.NewScheduler(time.UTC)

        // Do jobs with params
        s1.Every(1).Day().At("1:00").StartImmediately().Do(taskWithParams, 1, "hello")

        s1.StartBlocking()
}

Running this results in the following:

panic: reflect: call of reflect.Value.Type on zero Value

goroutine 5 [running]:
reflect.Value.Type(0x0, 0x0, 0x0, 0x0, 0x0)
        /Users/me/opt/go/src/reflect/value.go:1872 +0x183
github.com/go-co-op/gocron.callJobFuncWithParams(0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0)
        /Users/me/go/src/github.com/go-co-op/gocron/gocron.go:53 +0xc5
github.com/go-co-op/gocron.(*Job).run(0xc0000c4000)
        /Users/me/go/src/github.com/go-co-op/gocron/job.go:43 +0xd4
created by github.com/go-co-op/gocron.(*Scheduler).run
        /Users/me/go/src/github.com/go-co-op/gocron/scheduler.go:185 +0x167

Not particularly descriptive. :(

Steps to reproduce the behavior:

  1. go build
  2. run the above

Version

25952fd

Expected behavior

You should find a way to present a meaningful error message to the user here, even if you DO have to panic.

@flowchartsman flowchartsman added the bug Something isn't working label Sep 3, 2020
@JohnRoesler
Copy link
Contributor

JohnRoesler commented Sep 3, 2020

Looks like it comes down to bad examples by us not showing error checking when creating the schedule and our regex that parses time did not accept hours without leading zeros - fix incoming. Thanks for calling this out!

It's erroring when the At() func is called and so the job is nil which is causing the panic.

If you do this, it will fatal out with an error:

package main

import (
	"fmt"
	"log"
	"time"

	"github.com/go-co-op/gocron"
)

func taskWithParams(a int, b string) {
	fmt.Println(a, b)
}

func main() {
	// defines a new scheduler that schedules and runs jobs
	s1 := gocron.NewScheduler(time.UTC)

	// Do jobs with params
	_, err := s1.Every(1).Day().At("1:00").StartImmediately().Do(taskWithParams, 1, "hello")
	if err != nil {
		log.Fatalf("error creating schedule: %v", err)
	}

	s1.StartBlocking()
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants