chore: Linter rule for properly formatted api errors (#2123)

* chore: Linter rule for properly formatted api errors
* Add omitempty to 'Detail' field
This commit is contained in:
Steven Masley
2022-06-07 09:33:06 -05:00
committed by GitHub
parent 3f1e885d21
commit af401e3fe1
37 changed files with 351 additions and 302 deletions

View File

@ -26,7 +26,7 @@ func (api *API) postWorkspaceAuthAzureInstanceIdentity(rw http.ResponseWriter, r
instanceID, err := azureidentity.Validate(r.Context(), req.Signature, api.AzureCertificates)
if err != nil {
httpapi.Write(rw, http.StatusUnauthorized, httpapi.Response{
Message: "Invalid Azure identity",
Message: "Invalid Azure identity.",
Detail: err.Error(),
})
return
@ -45,7 +45,7 @@ func (api *API) postWorkspaceAuthAWSInstanceIdentity(rw http.ResponseWriter, r *
identity, err := awsidentity.Validate(req.Signature, req.Document, api.AWSCertificates)
if err != nil {
httpapi.Write(rw, http.StatusUnauthorized, httpapi.Response{
Message: "Invalid AWS identity",
Message: "Invalid AWS identity.",
Detail: err.Error(),
})
return
@ -66,7 +66,7 @@ func (api *API) postWorkspaceAuthGoogleInstanceIdentity(rw http.ResponseWriter,
payload, err := api.GoogleTokenValidator.Validate(r.Context(), req.JSONWebToken, "")
if err != nil {
httpapi.Write(rw, http.StatusUnauthorized, httpapi.Response{
Message: "Invalid GCP identity",
Message: "Invalid GCP identity.",
Detail: err.Error(),
})
return
@ -81,7 +81,7 @@ func (api *API) postWorkspaceAuthGoogleInstanceIdentity(rw http.ResponseWriter,
err = mapstructure.Decode(payload.Claims, &claims)
if err != nil {
httpapi.Write(rw, http.StatusBadRequest, httpapi.Response{
Message: "Error decoding JWT claims",
Message: "Error decoding JWT claims.",
Detail: err.Error(),
})
return
@ -93,13 +93,13 @@ func (api *API) handleAuthInstanceID(rw http.ResponseWriter, r *http.Request, in
agent, err := api.Database.GetWorkspaceAgentByInstanceID(r.Context(), instanceID)
if errors.Is(err, sql.ErrNoRows) {
httpapi.Write(rw, http.StatusNotFound, httpapi.Response{
Message: fmt.Sprintf("Instance with id %q not found", instanceID),
Message: fmt.Sprintf("Instance with id %q not found.", instanceID),
})
return
}
if err != nil {
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
Message: "Internal error fetching provisioner job agent",
Message: "Internal error fetching provisioner job agent.",
Detail: err.Error(),
})
return
@ -107,7 +107,7 @@ func (api *API) handleAuthInstanceID(rw http.ResponseWriter, r *http.Request, in
resource, err := api.Database.GetWorkspaceResourceByID(r.Context(), agent.ResourceID)
if err != nil {
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
Message: "Internal error fetching provisioner job resource",
Message: "Internal error fetching provisioner job resource.",
Detail: err.Error(),
})
return
@ -115,14 +115,14 @@ func (api *API) handleAuthInstanceID(rw http.ResponseWriter, r *http.Request, in
job, err := api.Database.GetProvisionerJobByID(r.Context(), resource.JobID)
if err != nil {
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
Message: "Internal error fetching provisioner job",
Message: "Internal error fetching provisioner job.",
Detail: err.Error(),
})
return
}
if job.Type != database.ProvisionerJobTypeWorkspaceBuild {
httpapi.Write(rw, http.StatusBadRequest, httpapi.Response{
Message: fmt.Sprintf("%q jobs cannot be authenticated", job.Type),
Message: fmt.Sprintf("%q jobs cannot be authenticated.", job.Type),
})
return
}
@ -130,7 +130,7 @@ func (api *API) handleAuthInstanceID(rw http.ResponseWriter, r *http.Request, in
err = json.Unmarshal(job.Input, &jobData)
if err != nil {
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
Message: "Internal error extracting job data",
Message: "Internal error extracting job data.",
Detail: err.Error(),
})
return
@ -138,7 +138,7 @@ func (api *API) handleAuthInstanceID(rw http.ResponseWriter, r *http.Request, in
resourceHistory, err := api.Database.GetWorkspaceBuildByID(r.Context(), jobData.WorkspaceBuildID)
if err != nil {
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
Message: "Internal error fetching workspace build",
Message: "Internal error fetching workspace build.",
Detail: err.Error(),
})
return
@ -149,14 +149,14 @@ func (api *API) handleAuthInstanceID(rw http.ResponseWriter, r *http.Request, in
latestHistory, err := api.Database.GetLatestWorkspaceBuildByWorkspaceID(r.Context(), resourceHistory.WorkspaceID)
if err != nil {
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
Message: "Internal error fetching the latest workspace build",
Message: "Internal error fetching the latest workspace build.",
Detail: err.Error(),
})
return
}
if latestHistory.ID != resourceHistory.ID {
httpapi.Write(rw, http.StatusBadRequest, httpapi.Response{
Message: fmt.Sprintf("Resource found for id %q, but isn't registered on the latest history", instanceID),
Message: fmt.Sprintf("Resource found for id %q, but isn't registered on the latest history.", instanceID),
})
return
}