Skip to content

Make jobCancelError and jobSnoozeError public #625

@jace-ys

Description

@jace-ys

Hey 👋🏻 I'm not entirely sure why jobCancelError and jobSnoozeError are not exported as part of the public API, especially because jobCancelError also implements errors.Is and errors.Unwrap.

What I'm trying to achieve is to wrap my actual Work functions with an instrumented Work helper that logs/traces job errors. Right now the only way to check if the actual Work returned river.JobCancel or river.JobSnooze is to do a string prefix match, which isn't ideal.

For example:

	err = w.worker.Work(ctx, job)
	if err != nil {
		var errReason string
		switch {
		case strings.HasPrefix(err.Error(), "jobCancelError:"):
			errReason = "job cancelled"
			span.SetAttributes(attribute.Bool("job.cancelled", true))

		case strings.HasPrefix(err.Error(), "jobSnoozeError:"):
			slog.Info(ctx, "job snoozed")
			span.SetAttributes(attribute.Bool("job.snoozed", true))
			return err

		case job.Attempt == job.MaxAttempts:
			errReason = "job failed, discarded due to max attempts exceeded"
			span.SetAttributes(attribute.Bool("job.discarded", true))

		default:
			errReason = "job failed"
			span.SetAttributes(attribute.Bool("job.failed", true))
		}

		slog.Error(ctx, errReason, err)
		span.SetStatus(codes.Error, errReason)
		span.SetAttributes(attribute.String("error", err.Error()))

		return err
	}

If the error types were made public, I will be able to simplify this using errors.Is(err, &river.JobCancelError{}).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions