Skip to content

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Mar 8, 2023
Merged

feat: propagate job error codes #6507

merged 9 commits into from
Mar 8, 2023

Conversation

mtojek
Copy link
Member

@mtojek mtojek commented Mar 8, 2023

Related: #6100

This PR extends a provisioner job runner to return error codes related to specificc failures. So far we need two different codes:
MISSING_TEMPLATE_PARAMETER and REQUIRED_TEMPLATE_VARIABLES.

@mtojek mtojek self-assigned this Mar 8, 2023
@mtojek mtojek requested review from BrunoQuaresma and mafredri March 8, 2023 13:31
@mtojek mtojek marked this pull request as ready for review March 8, 2023 13:31
@mtojek mtojek requested a review from kylecarbs March 8, 2023 13:32
Copy link
Collaborator

@BrunoQuaresma BrunoQuaresma left a comment

Choose a reason for hiding this comment

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

That is nice! Would be great to have the errors being exported on typesGenerated as:

export const Errors = {
  REQUIRED_TEMPLATE_VARIABLES: "REQUIRED_TEMPLATE_VARIABLES",
  ...
}

So we could easily find out what are the available error codes and use as:

someError === Errors.REQUIRED_TEMPLATE_VARIABLES

Copy link
Member

@mafredri mafredri left a comment

Choose a reason for hiding this comment

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

A few comments but looks good otherwise 👍🏻

@@ -0,0 +1 @@
ALTER TABLE provisioner_jobs ADD COLUMN error_code text DEFAULT NULL;
Copy link
Member

Choose a reason for hiding this comment

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

Could we potentially run into multiple errors? Would it make sense to store an array here?

Copy link
Member Author

Choose a reason for hiding this comment

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

I would stick to the error_code at the moment. Most (all?) flows fail fast (return failJob(...)), so there is no simple way to aggregate multiple errors.
Moreover, we have single error property. In this case, we would need errors [] as well.

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?

var errorCodes = map[string]string{
	 MissingParameterErrorCode: missingParameterErrorText,
	// ...
}

for c, m := range errorCodes {
	// ...
}

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}.

Copy link
Member Author

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...

@mtojek
Copy link
Member Author

mtojek commented Mar 8, 2023

@BrunoQuaresma I added these errors to typesGenerated, but wasn't able to create a map, only an array. Could you please link an enum where it works as intended?

var _ error = new(ProvisionerJobError)

func (err *ProvisionerJobError) Error() string {
return err.Message
Copy link
Member

Choose a reason for hiding this comment

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

Just an idea, could be useful sometime.

Suggested change
return err.Message
return fmt.Sprintf("%s (%s)", err.Message, err.Code)

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for suggestion, but this message will be shown in CLI or site, let's not scare the customer :)

@@ -75,6 +83,7 @@ type ProvisionerJob struct {
CompletedAt *time.Time `json:"completed_at,omitempty" format:"date-time"`
CanceledAt *time.Time `json:"canceled_at,omitempty" format:"date-time"`
Error string `json:"error,omitempty"`
ErrorCode JobErrorCode `json:"error_code,omitempty" enums:"MISSING_TEMPLATE_PARAMETER,REQUIRED_TEMPLATE_VARIABLES"`
Copy link
Member

Choose a reason for hiding this comment

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

Question: Is the enums: still required for code gen even after the update of swaggo? In my experience it has been able to auto-detect but it might be very specific.

Copy link
Member Author

Choose a reason for hiding this comment

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

I checked it now - unfortunately, swaggo removed enum parts from the docs and swagger.json, so I will leave it as is.

@mtojek mtojek merged commit 3b87316 into coder:main Mar 8, 2023
@github-actions github-actions bot locked and limited conversation to collaborators Mar 8, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants