| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "fmt" |
| 6 | "maps" |
| 7 | |
| 8 | "reasonix/internal/control" |
| 9 | "reasonix/internal/jobs" |
| 10 | ) |
| 11 | |
| 12 | type ModelApplicationDetails = control.ModelApplicationDetails |
| 13 | |
| 14 | func modelApplicationDetails(ctrl control.SessionAPI) *ModelApplicationDetails { |
| 15 | d := &ModelApplicationDetails{Code: "model_settings_pending", BlockingJobs: []jobs.View{}} |
| 16 | defer d.SetAvailableActions() |
| 17 | if c, ok := ctrl.(*control.Controller); ok { |
| 18 | if scope := c.BackgroundScope(); scope != nil { |
| 19 | d.Applying = scope.Manager.ReplacementInProgress() |
| 20 | } |
| 21 | d.Model = c.ModelRef() |
| 22 | d.ConnectionTarget = c.ModelConnectionTarget() |
| 23 | d.RuntimeIdentity = c.ModelRuntimeIdentity() |
| 24 | var err error |
| 25 | d.AppliedRevision, d.DesiredRevision, err = c.ModelSettingsState() |
| 26 | if err != nil { |
| 27 | d.Code = "model_settings_read_failed" |
| 28 | return d |
| 29 | } |
| 30 | d.BlockingJobs = append(d.BlockingJobs, c.ModelReplacementJobs()...) |
| 31 | if err := c.ValidateModelContinuation(); err != nil { |
| 32 | d.ContinuationUnavailable = modelSettingsIssue("continuation_unavailable", err).Message |
| 33 | } else { |
| 34 | d.CanUseApplied = true |
| 35 | } |
| 36 | } |
| 37 | return d |
| 38 | } |
| 39 | |
| 40 | type modelApplicationError struct { |
| 41 | cause error |
| 42 | details *ModelApplicationDetails |
| 43 | } |
| 44 | |
| 45 | func (e *modelApplicationError) Error() string { |
| 46 | return fmt.Sprintf("model settings were saved but this session could not apply them: %v", e.cause) |
| 47 | } |
| 48 | func (e *modelApplicationError) Unwrap() error { return e.cause } |
| 49 | func (e *modelApplicationError) RPCErrorData() map[string]any { |
| 50 | return map[string]any{"kind": "model_application", "modelApplication": e.details} |
| 51 | } |
| 52 | |
| 53 | func newModelApplicationError(ctrl control.SessionAPI, err error) error { |
| 54 | d := modelApplicationDetails(ctrl) |
| 55 | var busy *rebuildBusyError |
| 56 | if !errors.As(err, &busy) { |
| 57 | d.Code = "model_settings_apply_failed" |
| 58 | } |
| 59 | if errors.Is(err, control.ErrModelChoiceStale) { |
| 60 | d.Code = "model_choice_stale" |
| 61 | } |
| 62 | if errors.Is(err, control.ErrModelContinuationDenied) { |
| 63 | d.Code = "model_continuation_denied" |
| 64 | } |
| 65 | return &modelApplicationError{cause: err, details: d} |
| 66 | } |
| 67 | |
| 68 | type submissionNotAcceptedError struct{ cause error } |
| 69 | |
| 70 | func (e *submissionNotAcceptedError) Error() string { |
| 71 | return fmt.Sprintf("submission not accepted\n%v", e.cause) |
| 72 | } |
| 73 | func (e *submissionNotAcceptedError) Unwrap() []error { |
| 74 | return []error{control.ErrSubmissionNotAccepted, e.cause} |
| 75 | } |
| 76 | func (e *submissionNotAcceptedError) RPCErrorData() map[string]any { |
| 77 | data := map[string]any{"submissionOutcome": "not_accepted"} |
| 78 | var detailed interface{ RPCErrorData() map[string]any } |
| 79 | if errors.As(e.cause, &detailed) { |
| 80 | maps.Copy(data, detailed.RPCErrorData()) |
| 81 | } |
| 82 | return data |
| 83 | } |
| 84 |