-
Notifications
You must be signed in to change notification settings - Fork 7
RFC 9457 implementation at wfm client side #423
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ec8c5fb
feat(sbi): added problem types helper for rfc9457 implementation
vireshnavalli cdb186b
feat(problem-types): added problem types for all client responses
vireshnavalli c41545a
feat(problem-helpers): modified to include error fields
vireshnavalli a07859d
feat: added problem details changes for 303 and 4xx/5xx
vireshnavalli c53254f
fix: removes retryable for 500 response code
vireshnavalli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,253 @@ | ||
| // sandbox/standard/generatedCode/wfm/sbi/problem_helpers.go | ||
| // Hand-written helpers for the generated ProblemDetail type. DO NOT regenerate. | ||
| package sbi | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
| "io" | ||
| "net/http" | ||
| "strconv" | ||
| ) | ||
|
|
||
| // ── Margo-reserved problem type URIs ───────────────────────────────────────── | ||
| // Source: https://docs.margo.org/specification/problem-types | ||
| // Stable, unversioned identifiers. Clients MUST use type URI for programmatic | ||
| // error handling — NOT the status code or title. | ||
| const ( | ||
| ProblemBaseURI = "https://docs.margo.org/specification/problem-types" | ||
|
|
||
| // 400 — Malformed request body. | ||
| ProblemTypeInvalidRequest = ProblemBaseURI + "#invalid-request" | ||
|
|
||
| // 403 — Request not authorized by WFM local policy. | ||
| ProblemTypeNotAuthorized = ProblemBaseURI + "#not-authorized" | ||
|
|
||
| // 404 — No gateway found for the given child-device deviceId. | ||
| ProblemTypeGatewayNotFound = ProblemBaseURI + "#gateway-not-found" | ||
|
|
||
| // 404 — No device with the given deviceId found for the client. | ||
| ProblemTypeDeviceNotFound = ProblemBaseURI + "#device-not-found" | ||
|
|
||
| // 404 — Bundle not found for the given digest. | ||
| ProblemTypeInvalidBundle = ProblemBaseURI + "#invalid-bundle" | ||
|
|
||
| // 404 — Deployment not found for the given digest. | ||
| ProblemTypeDeploymentNotFound = ProblemBaseURI + "#deployment-not-found" | ||
|
|
||
| // 404 — Trust domain discovery document not available. | ||
| ProblemTypeDiscoveryDocumentNotFound = ProblemBaseURI + "#discovery-document-not-found" | ||
|
|
||
| // 404 — SPIFFE bundle unavailable. | ||
| ProblemTypeSpiffeBundleNotFound = ProblemBaseURI + "#spiffe-bundle-not-found" | ||
|
|
||
| // 406 — Server cannot generate a response matching the Accept header. | ||
| ProblemTypeServerCannotGenerateResponse = ProblemBaseURI + "#server-cannot-generate-response" | ||
|
|
||
| // 422 — Request body syntactically valid but contains a semantic error. | ||
| ProblemTypeSemanticError = ProblemBaseURI + "#semantic-error" | ||
| ) | ||
|
|
||
| // ── Non-Margo (about:blank) ─────────────────────────────────────────────────── | ||
| // Used when no Margo-specific problem type applies (RFC 9457 §4.2). | ||
| const ( | ||
| ProblemTypeAboutBlank = "about:blank" | ||
| ) | ||
|
|
||
| // ProblemContentType is the RFC 9457 media type. | ||
| const ProblemContentType = "application/problem+json" | ||
|
|
||
| // FieldError represents a single field-level validation error. | ||
| type FieldError struct { | ||
| Field string `json:"field"` | ||
| Message string `json:"message"` | ||
| } | ||
|
|
||
| // ── error interface ─────────────────────────────────────────────────────────── | ||
|
|
||
| func (p *ProblemDetail) Error() string { | ||
| if p.Detail != nil && *p.Detail != "" { | ||
| return fmt.Sprintf("[%d] %s: %s", p.Status, p.Title, *p.Detail) | ||
| } | ||
| return fmt.Sprintf("[%d] %s", p.Status, p.Title) | ||
| } | ||
|
|
||
| func (p *ProblemDetail) IsRetryable() bool { | ||
| return p.Retryable != nil && *p.Retryable | ||
| } | ||
|
|
||
| func (p *ProblemDetail) ShouldRetry() bool { | ||
| return p.IsRetryable() | ||
| } | ||
|
|
||
| // ── Builder ─────────────────────────────────────────────────────────────────── | ||
|
|
||
| func NewProblemDetail(problemType, title string, status int) *ProblemDetail { | ||
| return &ProblemDetail{Type: problemType, Title: title, Status: status} | ||
| } | ||
|
|
||
| func (p *ProblemDetail) WithDetail(d string) *ProblemDetail { | ||
| p.Detail = &d | ||
| return p | ||
| } | ||
|
|
||
| func (p *ProblemDetail) WithInstance(i string) *ProblemDetail { | ||
| p.Instance = &i | ||
| return p | ||
| } | ||
|
|
||
| func (p *ProblemDetail) WithRetryable(r bool) *ProblemDetail { | ||
| p.Retryable = &r | ||
| return p | ||
| } | ||
|
|
||
| func (p *ProblemDetail) WithRetryAfterSeconds(s int) *ProblemDetail { | ||
| p.RetryAfterSeconds = &s | ||
| return p | ||
| } | ||
|
|
||
| func (p *ProblemDetail) WithBackoffStrategy(s ProblemDetailBackoffStrategy) *ProblemDetail { | ||
| p.BackoffStrategy = &s | ||
| return p | ||
| } | ||
|
|
||
| // ── Convenience constructors ────────────────────────────────────────────────── | ||
|
|
||
| func NewInvalidRequest(detail, instance string) *ProblemDetail { | ||
| return NewProblemDetail(ProblemTypeInvalidRequest, "Invalid Request", http.StatusBadRequest). | ||
| WithDetail(detail).WithInstance(instance). | ||
| WithRetryable(false).WithBackoffStrategy(None) | ||
| } | ||
|
|
||
| func NewSemanticError(detail, instance string, fieldErrors ...FieldError) *ProblemDetail { | ||
| pd := NewProblemDetail(ProblemTypeSemanticError, "Semantic Error", http.StatusUnprocessableEntity). | ||
| WithDetail(detail).WithInstance(instance). | ||
| WithRetryable(false).WithBackoffStrategy(None) | ||
| if len(fieldErrors) > 0 { | ||
| errs := make([]struct { | ||
| Field *string `json:"field,omitempty"` | ||
| Message *string `json:"message,omitempty"` | ||
| }, len(fieldErrors)) | ||
| for i, fe := range fieldErrors { | ||
| f, m := fe.Field, fe.Message | ||
| errs[i].Field = &f | ||
| errs[i].Message = &m | ||
| } | ||
| pd.Errors = &errs | ||
| } | ||
| return pd | ||
| } | ||
|
|
||
|
|
||
| func NewNotAuthorized(detail, instance string) *ProblemDetail { | ||
| return NewProblemDetail(ProblemTypeNotAuthorized, "Not Authorized", http.StatusForbidden). | ||
| WithDetail(detail).WithInstance(instance). | ||
| WithRetryable(false).WithBackoffStrategy(None) | ||
| } | ||
|
|
||
| func NewGatewayNotFound(detail, instance string) *ProblemDetail { | ||
| return NewProblemDetail(ProblemTypeGatewayNotFound, "Gateway Not Found", http.StatusNotFound). | ||
| WithDetail(detail).WithInstance(instance). | ||
| WithRetryable(false).WithBackoffStrategy(None) | ||
| } | ||
|
|
||
| func NewDeviceNotFound(detail, instance string) *ProblemDetail { | ||
| return NewProblemDetail(ProblemTypeDeviceNotFound, "Device Not Found", http.StatusNotFound). | ||
| WithDetail(detail).WithInstance(instance). | ||
| WithRetryable(false).WithBackoffStrategy(None) | ||
| } | ||
|
|
||
| func NewInvalidBundle(detail, instance string) *ProblemDetail { | ||
| return NewProblemDetail(ProblemTypeInvalidBundle, "Invalid Bundle", http.StatusNotFound). | ||
| WithDetail(detail).WithInstance(instance). | ||
| WithRetryable(false).WithBackoffStrategy(None) | ||
| } | ||
|
|
||
| func NewDeploymentNotFound(detail, instance string) *ProblemDetail { | ||
| return NewProblemDetail(ProblemTypeDeploymentNotFound, "Deployment Not Found", http.StatusNotFound). | ||
| WithDetail(detail).WithInstance(instance). | ||
| WithRetryable(false).WithBackoffStrategy(None) | ||
| } | ||
|
|
||
| func NewServerCannotGenerateResponse(detail, instance string) *ProblemDetail { | ||
| return NewProblemDetail(ProblemTypeServerCannotGenerateResponse, "Server Cannot Generate Response", http.StatusNotAcceptable). | ||
| WithDetail(detail).WithInstance(instance). | ||
| WithRetryable(false).WithBackoffStrategy(None) | ||
| } | ||
|
|
||
| func NewInternalError(detail, instance string) *ProblemDetail { | ||
| return NewProblemDetail(ProblemTypeAboutBlank, "Internal Server Error", http.StatusInternalServerError). | ||
| WithDetail(detail).WithInstance(instance). | ||
| WithRetryable(false).WithBackoffStrategy(None) | ||
| } | ||
|
|
||
| func NewServiceUnavailable(detail, instance string) *ProblemDetail { | ||
| return NewProblemDetail(ProblemTypeAboutBlank, "Service Unavailable", http.StatusServiceUnavailable). | ||
| WithDetail(detail).WithInstance(instance). | ||
| WithRetryable(false).WithBackoffStrategy(None) | ||
| } | ||
|
|
||
| func NewConflict(detail, instance string) *ProblemDetail { | ||
| return NewProblemDetail(ProblemTypeAboutBlank, "Conflict", http.StatusConflict). | ||
| WithDetail(detail).WithInstance(instance). | ||
| WithRetryable(false).WithBackoffStrategy(None) | ||
| } | ||
|
|
||
| func NewTooManyRequests(detail, instance string, retryAfterSeconds int) *ProblemDetail { | ||
| return NewProblemDetail(ProblemTypeAboutBlank, "Too Many Requests", http.StatusTooManyRequests). | ||
| WithDetail(detail).WithInstance(instance). | ||
| WithRetryable(true).WithBackoffStrategy(Exponential). | ||
| WithRetryAfterSeconds(retryAfterSeconds) | ||
| } | ||
|
|
||
| func NewNotImplemented(detail, instance string) *ProblemDetail { | ||
| return NewProblemDetail(ProblemTypeAboutBlank, "Not Implemented", http.StatusNotImplemented). | ||
| WithDetail(detail).WithInstance(instance). | ||
| WithRetryable(false).WithBackoffStrategy(None) | ||
| } | ||
|
|
||
| // ── HTTP writer ─────────────────────────────────────────────────────────────── | ||
|
|
||
| func (p *ProblemDetail) WriteHTTP(w http.ResponseWriter) { | ||
| b, err := p.MarshalJSON() | ||
| if err != nil { | ||
| http.Error(w, http.StatusText(http.StatusInternalServerError), | ||
| http.StatusInternalServerError) | ||
| return | ||
| } | ||
| w.Header().Set("Content-Type", ProblemContentType) | ||
| if p.RetryAfterSeconds != nil { | ||
| w.Header().Set("Retry-After", strconv.Itoa(*p.RetryAfterSeconds)) | ||
| } | ||
| w.WriteHeader(p.Status) | ||
| _, _ = w.Write(b) | ||
| } | ||
|
|
||
| // ── Client-side helpers ─────────────────────────────────────────────────────── | ||
|
|
||
| func ParseErrorResponse(resp *http.Response) error { | ||
| if resp == nil { | ||
| return nil | ||
| } | ||
| if resp.StatusCode == http.StatusNotModified || | ||
| (resp.StatusCode >= 200 && resp.StatusCode < 300) { | ||
| return nil | ||
| } | ||
| body, err := io.ReadAll(resp.Body) | ||
| if err != nil { | ||
| return fmt.Errorf("HTTP %d: failed to read error body: %w", resp.StatusCode, err) | ||
| } | ||
| if resp.Header.Get("Content-Type") == ProblemContentType { | ||
| var pd ProblemDetail | ||
| if jsonErr := json.Unmarshal(body, &pd); jsonErr == nil { | ||
| return &pd | ||
| } | ||
| } | ||
| return fmt.Errorf("HTTP %d: %s", resp.StatusCode, string(body)) | ||
| } | ||
|
|
||
| func AsProblemDetail(err error) (*ProblemDetail, bool) { | ||
| var pd *ProblemDetail | ||
| return pd, errors.As(err, &pd) | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.