Skip to content

Commit 3b87316

Browse files
authored
feat: propagate job error codes (#6507)
* feat: propagate job error_code * fix * Fix * Fix * Fix * add errors to typesGenerated * Address PR comments * Fix
1 parent 524b14a commit 3b87316

25 files changed

+406
-234
lines changed

cli/cliui/provisionerjob.go

+15-1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,17 @@ type ProvisionerJobOptions struct {
4141
Silent bool
4242
}
4343

44+
type ProvisionerJobError struct {
45+
Message string
46+
Code codersdk.JobErrorCode
47+
}
48+
49+
var _ error = new(ProvisionerJobError)
50+
51+
func (err *ProvisionerJobError) Error() string {
52+
return err.Message
53+
}
54+
4455
// ProvisionerJob renders a provisioner job with interactive cancellation.
4556
func ProvisionerJob(ctx context.Context, writer io.Writer, opts ProvisionerJobOptions) error {
4657
if opts.FetchInterval == 0 {
@@ -181,7 +192,10 @@ func ProvisionerJob(ctx context.Context, writer io.Writer, opts ProvisionerJobOp
181192
return nil
182193
case codersdk.ProvisionerJobFailed:
183194
}
184-
err = xerrors.New(job.Error)
195+
err = &ProvisionerJobError{
196+
Message: job.Error,
197+
Code: job.ErrorCode,
198+
}
185199
jobMutex.Unlock()
186200
flushLogBuffer()
187201
return err

cli/templatecreate.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cli
22

33
import (
4+
"errors"
45
"fmt"
56
"io"
67
"os"
@@ -196,7 +197,8 @@ func createValidTemplateVersion(cmd *cobra.Command, args createValidTemplateVers
196197
},
197198
})
198199
if err != nil {
199-
if !provisionerd.IsMissingParameterError(err.Error()) {
200+
var jobErr *cliui.ProvisionerJobError
201+
if errors.As(err, &jobErr) && !provisionerd.IsMissingParameterErrorCode(string(jobErr.Code)) {
200202
return nil, nil, err
201203
}
202204
}
@@ -233,7 +235,7 @@ func createValidTemplateVersion(cmd *cobra.Command, args createValidTemplateVers
233235
}
234236
}
235237

236-
if provisionerd.IsMissingParameterError(version.Job.Error) {
238+
if provisionerd.IsMissingParameterErrorCode(string(version.Job.ErrorCode)) {
237239
valuesBySchemaID := map[string]codersdk.ComputedParameter{}
238240
for _, parameterValue := range parameterValues {
239241
valuesBySchemaID[parameterValue.SchemaID.String()] = parameterValue

coderd/apidoc/docs.go

+22
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/apidoc/swagger.json

+16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/dbfake/databasefake.go

+1
Original file line numberDiff line numberDiff line change
@@ -3512,6 +3512,7 @@ func (q *fakeQuerier) UpdateProvisionerJobWithCompleteByID(_ context.Context, ar
35123512
job.UpdatedAt = arg.UpdatedAt
35133513
job.CompletedAt = arg.CompletedAt
35143514
job.Error = arg.Error
3515+
job.ErrorCode = arg.ErrorCode
35153516
q.provisionerJobs[index] = job
35163517
return nil
35173518
}

coderd/database/dump.sql

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE provisioner_jobs DROP COLUMN error_code;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE provisioner_jobs ADD COLUMN error_code text DEFAULT NULL;

coderd/database/models.go

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries.sql.go

+14-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/provisionerjobs.sql

+2-1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ UPDATE
9191
SET
9292
updated_at = $2,
9393
completed_at = $3,
94-
error = $4
94+
error = $4,
95+
error_code = $5
9596
WHERE
9697
id = $1;

coderd/provisionerdserver/provisionerdserver.go

+6
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ func (server *Server) AcquireJob(ctx context.Context, _ *proto.Empty) (*proto.Ac
113113
String: errorMessage,
114114
Valid: true,
115115
},
116+
ErrorCode: job.ErrorCode,
116117
})
117118
if err != nil {
118119
return xerrors.Errorf("update provisioner job: %w", err)
@@ -639,12 +640,17 @@ func (server *Server) FailJob(ctx context.Context, failJob *proto.FailedJob) (*p
639640
String: failJob.Error,
640641
Valid: failJob.Error != "",
641642
}
643+
job.ErrorCode = sql.NullString{
644+
String: failJob.ErrorCode,
645+
Valid: failJob.ErrorCode != "",
646+
}
642647

643648
err = server.Database.UpdateProvisionerJobWithCompleteByID(ctx, database.UpdateProvisionerJobWithCompleteByIDParams{
644649
ID: jobID,
645650
CompletedAt: job.CompletedAt,
646651
UpdatedAt: database.Now(),
647652
Error: job.Error,
653+
ErrorCode: job.ErrorCode,
648654
})
649655
if err != nil {
650656
return nil, xerrors.Errorf("update provisioner job: %w", err)

coderd/provisionerjobs.go

+1
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ func convertProvisionerJob(provisionerJob database.ProvisionerJob) codersdk.Prov
317317
ID: provisionerJob.ID,
318318
CreatedAt: provisionerJob.CreatedAt,
319319
Error: provisionerJob.Error.String,
320+
ErrorCode: codersdk.JobErrorCode(provisionerJob.ErrorCode.String),
320321
FileID: provisionerJob.FileID,
321322
Tags: provisionerJob.Tags,
322323
}

codersdk/provisionerdaemons.go

+9
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ const (
6767
ProvisionerJobFailed ProvisionerJobStatus = "failed"
6868
)
6969

70+
// JobErrorCode defines the error code returned by job runner.
71+
type JobErrorCode string
72+
73+
const (
74+
MissingTemplateParameter JobErrorCode = "MISSING_TEMPLATE_PARAMETER"
75+
RequiredTemplateVariables JobErrorCode = "REQUIRED_TEMPLATE_VARIABLES"
76+
)
77+
7078
// ProvisionerJob describes the job executed by the provisioning daemon.
7179
type ProvisionerJob struct {
7280
ID uuid.UUID `json:"id" format:"uuid"`
@@ -75,6 +83,7 @@ type ProvisionerJob struct {
7583
CompletedAt *time.Time `json:"completed_at,omitempty" format:"date-time"`
7684
CanceledAt *time.Time `json:"canceled_at,omitempty" format:"date-time"`
7785
Error string `json:"error,omitempty"`
86+
ErrorCode JobErrorCode `json:"error_code,omitempty" enums:"MISSING_TEMPLATE_PARAMETER,REQUIRED_TEMPLATE_VARIABLES"`
7887
Status ProvisionerJobStatus `json:"status" enums:"pending,running,succeeded,canceling,canceled,failed"`
7988
WorkerID *uuid.UUID `json:"worker_id,omitempty" format:"uuid"`
8089
FileID uuid.UUID `json:"file_id" format:"uuid"`

0 commit comments

Comments
 (0)