Use Docs error pages throughout the contract pilot
3 files changed,
+41
−41
internal/docs/contract_actions.go+15 −15internal/docs/contract_demo.go+16 −16internal/docs/contract_view.go+10 −10
CI passed — run details
diff --git a/internal/docs/contract_actions.go b/internal/docs/contract_actions.go| index 7da203a..eeddaa6 100644 |
| --- a/internal/docs/contract_actions.go |
| +++ b/internal/docs/contract_actions.go |
| @@ -99,12 +99,12 @@ func (d *contractDemo) action(w http.ResponseWriter, r *http.Request) { |
| contractHeaders(w) |
| r.Body = http.MaxBytesReader(w, r.Body, 64<<10) |
| if r.ParseForm() != nil { |
| - contractFail(w, r) |
| + d.fail(w, r) |
| return |
| } |
| row, s, e := d.read(r) |
| if e != nil { |
| - http.NotFound(w, r) |
| + d.h.fail(w, r, http.StatusNotFound, failDocument) |
| return |
| } |
| action := r.PathValue("action") // chi does not populate PathValue on all versions. |
| @@ -118,13 +118,13 @@ func (d *contractDemo) action(w http.ResponseWriter, r *http.Request) { |
| switch action { |
| case "commit": |
| if s.Stage != "received" || !d.browser(r, s) || s.Release == nil || s.Release.Expires <= now { |
| - contractFail(w, r) |
| + d.fail(w, r) |
| return |
| } |
| s.Stage = "editing" |
| case "preview": |
| if s.Stage != "editing" && s.Stage != "preview" { |
| - contractFail(w, r) |
| + d.fail(w, r) |
| return |
| } |
| s.Title = r.PostForm.Get("title") |
| @@ -135,12 +135,12 @@ func (d *contractDemo) action(w http.ResponseWriter, r *http.Request) { |
| s.Stage = "editing" |
| s.PDF = nil |
| s.PreviewID = "" |
| - d.page(w, row, s, "Changes were not saved. Check the field names, date and fee. Every field in the template must be selected in Fichas; use a fee such as 1250.00.") |
| + d.page(w, r, row, s, "Changes were not saved. Check the field names, date and fee. Every field in the template must be selected in Fichas; use a fee such as 1250.00.") |
| return |
| } |
| case "issue": |
| if s.Stage != "preview" || s.Release == nil || s.Release.Expires <= now || r.PostForm.Get("preview") != s.PreviewID || len(s.PDF) == 0 { |
| - contractFail(w, r) |
| + d.fail(w, r) |
| return |
| } |
| s.Stage = "issued" |
| @@ -150,14 +150,14 @@ func (d *contractDemo) action(w http.ResponseWriter, r *http.Request) { |
| setDemoChallenge(&s, sendCode, now) |
| case "resend": |
| if s.Stage != "issued" || now >= s.Issued+86400 || now-s.LastSent < 60 { |
| - contractFail(w, r) |
| + d.fail(w, r) |
| return |
| } |
| sendCode = contractRandom(16) |
| setDemoChallenge(&s, sendCode, now) |
| case "verify": |
| if s.Stage != "issued" || now >= s.Issued+86400 || s.ChallengeExpires <= now || s.Attempts >= 5 || s.Verified != 0 { |
| - contractFail(w, r) |
| + d.fail(w, r) |
| return |
| } |
| s.Attempts++ |
| @@ -167,7 +167,7 @@ func (d *contractDemo) action(w http.ResponseWriter, r *http.Request) { |
| } |
| case "sign": |
| if s.Stage != "issued" || s.Verified == 0 || s.ChallengeExpires <= now || now >= s.Issued+86400 || r.PostForm.Get("digest") != s.Digest || r.PostForm.Get("consent") != "yes" { |
| - contractFail(w, r) |
| + d.fail(w, r) |
| return |
| } |
| s.Stage = "completed" |
| @@ -176,21 +176,21 @@ func (d *contractDemo) action(w http.ResponseWriter, r *http.Request) { |
| s.ChallengeHash = "" |
| case "void": |
| if s.Stage != "issued" { |
| - contractFail(w, r) |
| + d.fail(w, r) |
| return |
| } |
| s.Stage = "voided" |
| s.ChallengeHash = "" |
| s.Verified = 0 |
| default: |
| - http.NotFound(w, r) |
| + d.h.fail(w, r, http.StatusNotFound, failDocument) |
| return |
| } |
| if sendCode != "" { |
| s.ChallengeHash = contractHash(sendCode + ":" + row.ID + ":" + s.Digest) |
| } |
| if e = d.save(r, row, s, expires); e != nil { |
| - contractFail(w, r) |
| + d.fail(w, r) |
| return |
| } |
| if sendCode != "" { |
| @@ -222,7 +222,7 @@ func (d *contractDemo) pdf(w http.ResponseWriter, r *http.Request) { |
| row, s, e := d.read(r) |
| _ = row |
| if e != nil || len(s.PDF) == 0 || s.Stage == "voided" || (s.Stage == "issued" && time.Now().Unix() >= s.Issued+86400) { |
| - http.NotFound(w, r) |
| + d.h.fail(w, r, http.StatusNotFound, failDocument) |
| return |
| } |
| contractHeaders(w) |
| @@ -233,13 +233,13 @@ func (d *contractDemo) pdf(w http.ResponseWriter, r *http.Request) { |
| func (d *contractDemo) evidence(w http.ResponseWriter, r *http.Request) { |
| row, s, e := d.read(r) |
| if e != nil || s.Stage != "completed" { |
| - http.NotFound(w, r) |
| + d.h.fail(w, r, http.StatusNotFound, failDocument) |
| return |
| } |
| evidence := map[string]any{"version": 1, "agreement": row.ID, "title": s.Title, "sha256": s.Digest, "signer": s.Address, "issued_at": time.Unix(s.Issued, 0).UTC(), "verified_at": time.Unix(s.Verified, 0).UTC(), "signed_at": time.Unix(s.Signed, 0).UTC(), "consent_version": 1, "consent": s.Consent, "source": s.Release.Source, "source_card": s.Release.Card, "source_revision": s.Release.Revision, "release": s.Release.ID, "assurance": "Email address control and affirmative issuer-only electronic acceptance. No PDF certificate signature."} |
| raw, e := json.Marshal(evidence) |
| if e != nil { |
| - contractFail(w, r) |
| + d.fail(w, r) |
| return |
| } |
| contractHeaders(w) |
diff --git a/internal/docs/contract_demo.go b/internal/docs/contract_demo.go| index 17b7fee..06c6885 100644 |
| --- a/internal/docs/contract_demo.go |
| +++ b/internal/docs/contract_demo.go |
| @@ -183,28 +183,28 @@ func contractHeaders(w http.ResponseWriter) { |
| w.Header().Set("Referrer-Policy", "no-referrer") |
| w.Header().Set("X-Content-Type-Options", "nosniff") |
| } |
| -func contractFail(w http.ResponseWriter, r *http.Request) { |
| +func (d *contractDemo) fail(w http.ResponseWriter, r *http.Request) { |
| contractHeaders(w) |
| - http.Error(w, "This contract request is unavailable or has changed. Return to Contracts and try again.", http.StatusConflict) |
| + d.h.fail(w, r, http.StatusConflict, failStatus) |
| } |
| func (d *contractDemo) start(w http.ResponseWriter, r *http.Request) { |
| owner, e := d.actor(r) |
| if e != nil { |
| - http.NotFound(w, r) |
| + d.h.fail(w, r, http.StatusNotFound, failDocument) |
| return |
| } |
| // At most 20 live preparations per owner bounds anonymous receipt storage. |
| var count int64 |
| d.h.store.db.Model(&contractRow{}).Where("owner_id = ? AND expires > ?", owner, time.Now().Unix()).Count(&count) |
| if count >= 20 { |
| - contractFail(w, r) |
| + d.fail(w, r) |
| return |
| } |
| secret := contractRandom(32) |
| row := contractRow{ID: uuid.NewString(), OwnerID: owner, Version: 1, Expires: time.Now().Add(30 * time.Minute).Unix()} |
| s := contractState{Stage: "connecting", Browser: contractHash(secret), Address: d.cfg.Importer, Title: "Service agreement", Date: time.Now().Format("2006-01-02"), Fee: "1250.00", Body: "Service agreement\n\nThis agreement is prepared for {{f_name}}.\n\nThe services begin on {{start_date}}. The agreed fee is {{fee}}.\n\nThe provider will deliver the services described and agreed by both parties.\n\nPrepared for review and acceptance by the issuer."} |
| if d.seal(&row, s) != nil || d.h.store.db.Create(&row).Error != nil { |
| - contractFail(w, r) |
| + d.fail(w, r) |
| return |
| } |
| http.SetCookie(w, &http.Cookie{Name: contractCookie, Value: secret, Path: "/contracts/" + row.ID, HttpOnly: true, Secure: true, SameSite: http.SameSiteLaxMode, MaxAge: 1800}) |
| @@ -213,54 +213,54 @@ func (d *contractDemo) start(w http.ResponseWriter, r *http.Request) { |
| func (d *contractDemo) receipt(w http.ResponseWriter, r *http.Request) { |
| contractHeaders(w) |
| if r.Method != "POST" || r.Header.Get("Origin") != d.cfg.Source { |
| - http.NotFound(w, r) |
| + d.h.fail(w, r, http.StatusNotFound, failDocument) |
| return |
| } |
| r.Body = http.MaxBytesReader(w, r.Body, 128<<10) |
| if r.ParseForm() != nil { |
| - contractFail(w, r) |
| + d.fail(w, r) |
| return |
| } |
| raw, e := base64.RawURLEncoding.DecodeString(r.PostForm.Get("payload")) |
| sig, se := base64.RawURLEncoding.DecodeString(r.PostForm.Get("signature")) |
| if e != nil || se != nil || len(raw) > 64<<10 || !ed25519.Verify(d.key, raw, sig) { |
| - contractFail(w, r) |
| + d.fail(w, r) |
| return |
| } |
| var rel contractRelease |
| if json.Unmarshal(raw, &rel, json.RejectUnknownMembers(true)) != nil { |
| - contractFail(w, r) |
| + d.fail(w, r) |
| return |
| } |
| now := time.Now().Unix() |
| if rel.Version != 1 || rel.Source != d.cfg.Source || rel.Destination != d.h.origin || rel.Team != d.cfg.Team || rel.Importer != d.cfg.Importer || rel.Purpose != contractPurpose || rel.Expires <= now || rel.Issued > now+60 || rel.Expires-rel.Issued > 1800 || rel.Expires <= rel.Issued || rel.Card < 1 || rel.Revision < 1 || len(rel.Fields) == 0 || len(rel.Fields) > 32 { |
| - contractFail(w, r) |
| + d.fail(w, r) |
| return |
| } |
| if _, e = uuid.Parse(rel.ID); e != nil { |
| - contractFail(w, r) |
| + d.fail(w, r) |
| return |
| } |
| if _, e = uuid.Parse(rel.Flow); e != nil { |
| - contractFail(w, r) |
| + d.fail(w, r) |
| return |
| } |
| seen := map[string]bool{} |
| for _, f := range rel.Fields { |
| if seen[f.Key] || !contractFieldKey.MatchString(f.Key) || f.Key == "fee" || f.Key == "start_date" || len(f.Value) > 8192 || len(f.Label) > 256 || (f.Kind != "text" && f.Kind != "date") { |
| - contractFail(w, r) |
| + d.fail(w, r) |
| return |
| } |
| seen[f.Key] = true |
| } |
| var row contractRow |
| if d.h.store.db.Where("id = ? AND expires > ?", rel.Flow, now).First(&row).Error != nil { |
| - contractFail(w, r) |
| + d.fail(w, r) |
| return |
| } |
| s, e := d.open(row) |
| if e != nil || s.Stage != "connecting" || s.Release != nil || s.Address != rel.Importer { |
| - contractFail(w, r) |
| + d.fail(w, r) |
| return |
| } |
| s.Release = &rel |
| @@ -273,7 +273,7 @@ func (d *contractDemo) receipt(w http.ResponseWriter, r *http.Request) { |
| expires = rel.Expires |
| } |
| if d.save(r, row, s, expires) != nil { |
| - contractFail(w, r) |
| + d.fail(w, r) |
| return |
| } |
| http.Redirect(w, r, "/contracts/"+row.ID, http.StatusSeeOther) |
diff --git a/internal/docs/contract_view.go b/internal/docs/contract_view.go| index eab380a..09e0f45 100644 |
| --- a/internal/docs/contract_view.go |
| +++ b/internal/docs/contract_view.go |
| @@ -28,12 +28,12 @@ type contractSummary struct{ ID, Title, Stage string } |
| func (d *contractDemo) home(w http.ResponseWriter, r *http.Request) { |
| owner, e := d.actor(r) |
| if e != nil { |
| - http.NotFound(w, r) |
| + d.h.fail(w, r, http.StatusNotFound, failDocument) |
| return |
| } |
| var rows []contractRow |
| if d.h.store.db.Where("owner_id = ? AND expires > ?", owner, time.Now().Unix()).Order("expires desc").Limit(20).Find(&rows).Error != nil { |
| - contractFail(w, r) |
| + d.fail(w, r) |
| return |
| } |
| v := contractViewData{Home: true, Source: d.cfg.Source, State: contractState{Address: d.cfg.Importer}} |
| @@ -42,30 +42,30 @@ func (d *contractDemo) home(w http.ResponseWriter, r *http.Request) { |
| v.Records = append(v.Records, contractSummary{row.ID, s.Title, s.Stage}) |
| } |
| } |
| - d.render(w, v) |
| + d.render(w, r, v) |
| } |
| func (d *contractDemo) show(w http.ResponseWriter, r *http.Request) { |
| row, s, e := d.read(r) |
| if e != nil { |
| - http.NotFound(w, r) |
| + d.h.fail(w, r, http.StatusNotFound, failDocument) |
| return |
| } |
| if (s.Stage == "connecting" || s.Stage == "received") && !d.browser(r, s) { |
| - http.NotFound(w, r) |
| + d.h.fail(w, r, http.StatusNotFound, failDocument) |
| return |
| } |
| if s.Stage == "issued" && s.ChallengeExpires <= time.Now().Unix() { |
| s.Verified = 0 |
| } |
| - d.page(w, row, s, "") |
| + d.page(w, r, row, s, "") |
| } |
| -func (d *contractDemo) page(w http.ResponseWriter, row contractRow, s contractState, message string) { |
| - d.render(w, contractViewData{ID: row.ID, State: s, Error: message, Source: d.cfg.Source, Connect: d.cfg.Source + "/contracts/prepare?flow=" + row.ID, Retention: row.Expires, Expired: s.Stage == "issued" && time.Now().Unix() >= s.Issued+86400}) |
| +func (d *contractDemo) page(w http.ResponseWriter, r *http.Request, row contractRow, s contractState, message string) { |
| + d.render(w, r, contractViewData{ID: row.ID, State: s, Error: message, Source: d.cfg.Source, Connect: d.cfg.Source + "/contracts/prepare?flow=" + row.ID, Retention: row.Expires, Expired: s.Stage == "issued" && time.Now().Unix() >= s.Issued+86400}) |
| } |
| -func (d *contractDemo) render(w http.ResponseWriter, v contractViewData) { |
| +func (d *contractDemo) render(w http.ResponseWriter, r *http.Request, v contractViewData) { |
| var buf bytes.Buffer |
| if e := contractView.Execute(&buf, v); e != nil { |
| - http.Error(w, "The contract page could not be shown.", 500) |
| + d.h.fail(w, r, http.StatusInternalServerError, failStatus) |
| return |
| } |
| contractHeaders(w) |