Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Refactor templateVersionDynamicParameters to remove control flag and send websocket errors #18478

Open
@Emyrk

Description

@Emyrk

This code currently operates with a control flag listen

coder/coderd/parameters.go

Lines 71 to 112 in ae82770

// The `listen` control flag determines whether to open a websocket connection to
// handle the request or not. This same function is used to 'evaluate' a template
// as a single invocation, or to 'listen' for a back and forth interaction with
// the user to update the form as they type.
//
//nolint:revive // listen is a control flag
func (api *API) templateVersionDynamicParameters(listen bool, initial codersdk.DynamicParametersRequest) func(rw http.ResponseWriter, r *http.Request) {
return func(rw http.ResponseWriter, r *http.Request) {
ctx := r.Context()
templateVersion := httpmw.TemplateVersionParam(r)
renderer, err := dynamicparameters.Prepare(ctx, api.Database, api.FileCache, templateVersion.ID,
dynamicparameters.WithTemplateVersion(templateVersion),
)
if err != nil {
if httpapi.Is404Error(err) {
httpapi.ResourceNotFound(rw)
return
}
if xerrors.Is(err, dynamicparameters.ErrTemplateVersionNotReady) {
httpapi.Write(ctx, rw, http.StatusTooEarly, codersdk.Response{
Message: "Template version job has not finished",
})
return
}
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
Message: "Internal error fetching template version data.",
Detail: err.Error(),
})
return
}
defer renderer.Close()
if listen {
api.handleParameterWebsocket(rw, r, initial, renderer)
} else {
api.handleParameterEvaluate(rw, r, initial, renderer)
}
}
}

This requires a nolint and should be fixed. Additionally, http errors are not well received by websocket based FE requests. These errors should be translated to the websocket message to be properly rendered on the FE.

Metadata

Metadata

Assignees

Labels

GAcandidate for a GA release

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions