fix: remove string TTL from workspace error responses (#3257)

- Rewrites some error messages to better integrate with the frontend (ttl_ms -> time until shutdown)
- Makes codersdk.ValidationError implement the error interface
- Only return validations if the error was a validation error, return detail otherwise (e.g. database error)
This commit is contained in:
Cian Johnston
2022-07-27 21:20:02 +00:00
committed by GitHub
parent 36ffdce065
commit 27ea415b6c
5 changed files with 34 additions and 27 deletions
+7
View File
@@ -1,6 +1,7 @@
package codersdk
import (
"fmt"
"net"
"golang.org/x/xerrors"
@@ -32,6 +33,12 @@ type ValidationError struct {
Detail string `json:"detail" validate:"required"`
}
func (e ValidationError) Error() string {
return fmt.Sprintf("field: %s detail: %s", e.Field, e.Detail)
}
var _ error = (*ValidationError)(nil)
// IsConnectionErr is a convenience function for checking if the source of an
// error is due to a 'connection refused', 'no such host', etc.
func IsConnectionErr(err error) bool {
+2 -2
View File
@@ -192,7 +192,7 @@ func (c *Client) UpdateWorkspaceTTL(ctx context.Context, id uuid.UUID, req Updat
path := fmt.Sprintf("/api/v2/workspaces/%s/ttl", id.String())
res, err := c.Request(ctx, http.MethodPut, path, req)
if err != nil {
return xerrors.Errorf("update workspace ttl: %w", err)
return xerrors.Errorf("update workspace time until shutdown: %w", err)
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
@@ -212,7 +212,7 @@ func (c *Client) PutExtendWorkspace(ctx context.Context, id uuid.UUID, req PutEx
path := fmt.Sprintf("/api/v2/workspaces/%s/extend", id.String())
res, err := c.Request(ctx, http.MethodPut, path, req)
if err != nil {
return xerrors.Errorf("extend workspace ttl: %w", err)
return xerrors.Errorf("extend workspace time until shutdown: %w", err)
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK && res.StatusCode != http.StatusNotModified {