Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat: propagate job error codes#6507

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
mtojek merged 9 commits intocoder:mainfrommtojek:6100-error-codes
Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
PrevPrevious commit
NextNext commit
fix
  • Loading branch information
@mtojek
mtojek committedMar 8, 2023
commitbfa914003c3fe4fc4dc260107700f7aefae13197
20 changes: 19 additions & 1 deletioncli/cliui/provisionerjob.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -41,6 +41,21 @@ type ProvisionerJobOptions struct {
Silent bool
}

type ProvisionerJobError struct {
message string
code string
}

var _ error = new(ProvisionerJobError)

func (err *ProvisionerJobError) Error() string {
return err.message
}

func (err *ProvisionerJobError) Code() string {
return err.code
}

// ProvisionerJob renders a provisioner job with interactive cancellation.
func ProvisionerJob(ctx context.Context, writer io.Writer, opts ProvisionerJobOptions) error {
if opts.FetchInterval == 0 {
Expand DownExpand Up@@ -181,7 +196,10 @@ func ProvisionerJob(ctx context.Context, writer io.Writer, opts ProvisionerJobOp
return nil
case codersdk.ProvisionerJobFailed:
}
err = xerrors.New(job.Error)
err = &ProvisionerJobError{
message: job.Error,
code: job.ErrorCode,
}
jobMutex.Unlock()
flushLogBuffer()
return err
Expand Down
5 changes: 3 additions & 2 deletionscli/templatecreate.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -196,7 +196,8 @@ func createValidTemplateVersion(cmd *cobra.Command, args createValidTemplateVers
},
})
if err != nil {
if !provisionerd.IsMissingParameterError(err.Error()) {
jobErr, isJobErr := err.(*cliui.ProvisionerJobError)
if isJobErr && !provisionerd.IsMissingParameterError(jobErr.Code()) {
return nil, nil, err
}
}
Expand DownExpand Up@@ -233,7 +234,7 @@ func createValidTemplateVersion(cmd *cobra.Command, args createValidTemplateVers
}
}

if provisionerd.IsMissingParameterError(version.Job.Error) {
if provisionerd.IsMissingParameterError(version.Job.ErrorCode) {
valuesBySchemaID := map[string]codersdk.ComputedParameter{}
for _, parameterValue := range parameterValues {
valuesBySchemaID[parameterValue.SchemaID.String()] = parameterValue
Expand Down
1 change: 1 addition & 0 deletionscoderd/database/dbfake/databasefake.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -3512,6 +3512,7 @@ func (q *fakeQuerier) UpdateProvisionerJobWithCompleteByID(_ context.Context, ar
job.UpdatedAt = arg.UpdatedAt
job.CompletedAt = arg.CompletedAt
job.Error = arg.Error
job.ErrorCode = arg.ErrorCode
q.provisionerJobs[index] = job
return nil
}
Expand Down
6 changes: 6 additions & 0 deletionscoderd/provisionerdserver/provisionerdserver.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -113,6 +113,7 @@ func (server *Server) AcquireJob(ctx context.Context, _ *proto.Empty) (*proto.Ac
String: errorMessage,
Valid: true,
},
ErrorCode: job.ErrorCode,
})
if err != nil {
return xerrors.Errorf("update provisioner job: %w", err)
Expand DownExpand Up@@ -639,12 +640,17 @@ func (server *Server) FailJob(ctx context.Context, failJob *proto.FailedJob) (*p
String: failJob.Error,
Valid: failJob.Error != "",
}
job.ErrorCode = sql.NullString{
String: failJob.ErrorCode,
Valid: failJob.ErrorCode != "",
}

err = server.Database.UpdateProvisionerJobWithCompleteByID(ctx, database.UpdateProvisionerJobWithCompleteByIDParams{
ID: jobID,
CompletedAt: job.CompletedAt,
UpdatedAt: database.Now(),
Error: job.Error,
ErrorCode: job.ErrorCode,
})
if err != nil {
return nil, xerrors.Errorf("update provisioner job: %w", err)
Expand Down
10 changes: 4 additions & 6 deletionsprovisionerd/provisionerd.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,7 +6,6 @@ import (
"fmt"
"io"
"reflect"
"strings"
"sync"
"time"

Expand All@@ -30,11 +29,10 @@ import (
"github.com/coder/retry"
)

// IsMissingParameterError returns whether the error message provided
// is a missing parameter error. This can indicate to consumers that
// they should check parameters.
func IsMissingParameterError(err string) bool {
return strings.Contains(err, runner.MissingParameterErrorText)
// IsMissingParameterError returns whether the error is a missing parameter error.
// This can indicate to consumers that they should check parameters.
func IsMissingParameterError(errorCode string) bool {
return errorCode == runner.MissingParameterErrorCode
}

// Dialer represents the function to create a daemon client connection.
Expand Down
25 changes: 14 additions & 11 deletionsprovisionerd/runner/runner.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -30,8 +30,11 @@ import (
)

const (
MissingParameterErrorCode = "TEMPLATE_MISSING_PARAMETER"
MissingParameterErrorText = "missing parameter"
MissingParameterErrorCode = "MISSING_TEMPLATE_PARAMETER"
missingParameterErrorText = "missing parameter"

RequiredTemplateVariablesErrorCode = "REQUIRED_TEMPLATE_VARIABLES"
requiredTemplateVariablesErrorText = "required template variables"
)

var errUpdateSkipped = xerrors.New("update skipped; job complete or failed")
Expand DownExpand Up@@ -590,8 +593,7 @@ func (r *Runner) runTemplateImport(ctx context.Context) (*proto.CompletedJob, *p
for _, parameterSchema := range parameterSchemas {
_, ok := valueByName[parameterSchema.Name]
if !ok {
return nil, r.failedJobWithCodef(MissingParameterErrorCode,
"%s: %s", MissingParameterErrorText, parameterSchema.Name)
return nil, r.failedJobf("%s: %s", missingParameterErrorText, parameterSchema.Name)
}
}

Expand DownExpand Up@@ -1053,16 +1055,17 @@ func (r *Runner) runWorkspaceBuild(ctx context.Context) (*proto.CompletedJob, *p
}

func (r *Runner) failedJobf(format string, args ...interface{}) *proto.FailedJob {
return &proto.FailedJob{
JobId: r.job.JobId,
Error: fmt.Sprintf(format, args...),
}
}
message := fmt.Sprintf(format, args...)
var code string

func (r *Runner) failedJobWithCodef(code, format string, args ...interface{}) *proto.FailedJob {
if strings.Contains(message, missingParameterErrorText) {
code = MissingParameterErrorCode
} else if strings.Contains(message, requiredTemplateVariablesErrorText) {
code = RequiredTemplateVariablesErrorCode
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

This format seems like it'd be easy to miss, e.g. when adding new error messages/codes.

How about storing them in a map and iterating over it here?

varerrorCodes=map[string]string{MissingParameterErrorCode:missingParameterErrorText,// ...}forc,m:=rangeerrorCodes {// ...}

Ultimately though, this logic is a bit hidden behind a "print function", it might be a good idea to refactor so that it can accept anerr error which could be a predefined&jobError{m: "missing x", c: "MISSING_X}.

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Good idea with the map, added.

Ultimately though, this logic is a bit hidden behind a "print function", it might be a good idea to refactor so that it can accept an err error which could be a predefined &jobError{m: "missing x", c: "MISSING_X}.

This is risky, as it would require refactoring of interfaces, and I'm sure that I don't want to deal with it now...

return &proto.FailedJob{
JobId: r.job.JobId,
Error:fmt.Sprintf(format, args...),
Error:message,
ErrorCode: code,
}
}
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -475,14 +475,15 @@ export const createTemplateMachine =

const isMissingParameter = (version: TemplateVersion) => {
return Boolean(
version.job.error && version.job.error.includes("missing parameter"),
version.job.error_code &&
version.job.error_code === "MISSING_TEMPLATE_PARAMETER",
)
}

const isMissingVariables = (version: TemplateVersion) => {
return Boolean(
version.job.error &&
version.job.error.includes("required template variables"),
version.job.error_code &&
version.job.error_code === "REQUIRED_TEMPLATE_VARIABLES",
)
}

Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp