A peer-to-peer stack for code collaboration
Extract errors from diff and issue app (#500)
Merle Breitkreuz6d3c771362ad
diff --git a/bin/rad-diff b/bin/rad-diffindex 72f6d3e..f683c3b 100755--- a/bin/rad-diff+++ b/bin/rad-diff@@ -5,6 +5,9 @@(load! (find-module-file! "prelude/io-utils.rad"))(load! (find-module-file! "monadic/project.rad"))+(file-module! "prelude/error-messages.rad")+(import prelude/error-messages :as 'error)+(import prelude/validation :as 'validation)(def machine-name (fn [] (get-rsm-of-type! :rad-diff)))@@ -62,7 +65,7 @@(fn [diff-no machine](match (lookup-default diff-no #f (list-diffs machine))#f (do- (put-str! (string-append "DIFF " (show diff-no) " does not exist."))+ (put-str! (error/item-not-found :diff (show diff-no)))(exit! 1))'the-diff the-diff)))@@ -73,7 +76,7 @@(match (process-with-stdout-stderr-exitcode! "git" ["show" commit] "")[_ _ :ok] commit_ (do- (put-str! "Commit can't be found")+ (put-str! (error/unknown-commit commit))(exit! 1)))))(def get-patch!@@ -108,15 +111,13 @@"Creates a new branch diff/<DIFF-ID> based on the DIFF, and switches to it."(fn [machine diff-no](def diff (verify-diff-number diff-no machine))+ (def diff-branch (string-append "diff/" (show diff-no)))(process-git-with-exit!- ["checkout" "-b" (string-append "diff/" (show diff-no))]- (string-append "Creating and switching to branch diff/" (show diff-no) "' failed."))+ ["checkout" "-b" diff-branch]+ (error/checkout-new-branch-failure diff-branch))(apply-patch!(lookup :patch diff)- (string-append- "Applying the patch failed.\n"- "Resolve any conflicts and run 'git am --continue', \n"- "or 'git am --abort' to abort."))))+ (error/applying-patch-failure))))(def push-diff!"Merges the DIFF to master, and pushes the changes."@@ -124,22 +125,13 @@(def diff (verify-diff-number diff-no machine))(process-git-with-exit!["checkout" "master"]- (string-append- "Checkout branch master failed.\n"- "Refusing to continue."))+ (error/checkout-master-failure))(apply-patch!(lookup :patch diff)- (string-append- "Applying the patch failed.\n"- "Resolve any conflicts and and then run 'git push', \n"- "or 'git am --abort' to abort."))+ (error/applying-accepted-patch-failure))(process-git-with-exit!["push"]- (string-append- "Pushing the diff failed.\n"- "This may be because you have no internet connection, "- "you are not authorized to push to origin, "- "or the remote is currently not reachable."))))+ (error/push-diff-failure))))(def new-diff!"Creates a new diff on `machine` with a patch from the given `commit`."@@ -160,7 +152,7 @@(match (simple-create-diff! machine diff)['n] (put-str! (string-append "Proposing #" (show n)" to " machine))- _ (put-str! "Diff proposed but unexpected results returned."))))+ _ (put-str! (error/no-number-returned :diff)))));; showing diffs@@ -318,8 +310,8 @@(do(def diff (verify-diff-number n machine))(put-str! (pretty-diff diff)))- (cmd-parse-failure "Diff numbers must be whole numbers!"))- (cmd-parse-failure "To show a diff, please provide the diff number."))))+ (cmd-parse-failure (error/whole-item-number :diff)))+ (cmd-parse-failure (error/missing-item-number :diff "show")))))(def edit-diff-state!"Edit the state of a DIFF. If the new state is `:accepted` the DIFF is then@@ -334,11 +326,7 @@(simple-edit-diff! machine n {:state new-state})(fn [x](put-str!- (string-append- "Diff could not be "- (pretty-state new-state)- ". Are you authorized to do so?"- ))+ (error/state-change-failure :diff (pretty-state new-state)))(exit! 1)))(put-str! message)))(verify-diff-number n machine)@@ -349,8 +337,8 @@_ (exit! 1))(edit-state-with-feedback!(string-append "Diff " (show n) " has been " (pretty-state new-state)))))- (cmd-parse-failure "Diff numbers must be whole numbers!"))- (cmd-parse-failure "To change the state, please provide the diff number."))))+ (cmd-parse-failure (error/whole-item-number :diff)))+ (cmd-parse-failure (error/missing-item-number :issue "change the state of")))))(def create-comment!"Add a new `comment` to DIFF."@@ -361,8 +349,8 @@(verify-diff-number n machine)(simple-add-comment! machine n comment (get-git-username!))(put-str! (string-append "Added comment to Diff " (show n))))- (cmd-parse-failure "Diff numbers must be whole numbers!"))- (cmd-parse-failure "To create a comment, please provide the diff number."))))+ (cmd-parse-failure (error/whole-item-number :diff)))+ (cmd-parse-failure (error/missing-item-number :issue "comment on")))))(def cmd-options[@@ -391,5 +379,5 @@["help"] (put-str! help)["-h"] (put-str! help)["--help"] (put-str! help)- (/cons 'cmd _) (cmd-parse-failure (string-append "Unknown command: " cmd))+ (/cons 'cmd _) (cmd-parse-failure (error/unknown-command cmd))[] (put-str! help))))diff --git a/bin/rad-issue b/bin/rad-issueindex b77bff8..779daf0 100755--- a/bin/rad-issue+++ b/bin/rad-issue@@ -5,6 +5,9 @@(load! (find-module-file! "monadic/project.rad"))(load! (find-module-file! "prelude/io-utils.rad"))+(file-module! "prelude/error-messages.rad")+(import prelude/error-messages :as 'error)+(import prelude/validation :as 'validation)(import prelude/machine :as 'machine)@@ -58,7 +61,7 @@(fn [issue-no machine](match (lookup-default issue-no #f (list-issues machine))#f (do- (put-str! (string-append "ISSUE " (show issue-no) " does not exist."))+ (put-str! (error/item-not-found :issue (show issue-no)))(exit! 1))'the-issue the-issue)))@@ -239,8 +242,8 @@(do(def issue (verify-issue-number n machine))(put-str! (pretty-issue (add-username-issue issue))))- (cmd-parse-failure "Issue numbers must be whole numbers!"))- (cmd-parse-failure "To show an issue, please provide the issue number."))))+ (cmd-parse-failure (error/whole-item-number :issue)))+ (cmd-parse-failure (error/missing-item-number :issue "show")))))(def close-issue(fn [issue-number machine]@@ -252,10 +255,10 @@(verify-issue-number n machine)(simple-edit-issue! machine n {:state :closed}))(fn [_]- (put-str! "Issue could not be closed. Are you authorized to do so?")+ (put-str! (error/state-change-failure :issue "closed"))(exit! 1)))- (cmd-parse-failure "Issue numbers must be whole numbers!"))- (cmd-parse-failure "To close an issue, please provide the issue number."))))+ (cmd-parse-failure (error/whole-item-number :issue)))+ (cmd-parse-failure (error/missing-item-number :issue "close")))))(def create-comment!"Add a new `comment` to ISSUE."@@ -266,8 +269,8 @@(verify-issue-number n machine)(simple-add-comment! machine n comment (get-git-username!))(put-str! (string-append "Added comment to issue " (show n))))- (cmd-parse-failure "Issue numbers must be whole numbers!"))- (cmd-parse-failure "To create a comment, please provide the issue number."))))+ (cmd-parse-failure (error/whole-item-number :issue)))+ (cmd-parse-failure (error/missing-item-number :issue "comment on")))))(def new-issue(fn [machine]@@ -282,7 +285,7 @@(def author {:git-username (get-git-username!)})(match (create-issue! machine (simple-issue (<> i author)))['n] (put-str! (string-append "Created issue #" (show n) " in " machine))- _ (put-str! "Issue created but unexpected results returned."))))+ _ (put-str! (error/no-number-returned :issue)))))(def cmd-options[@@ -307,5 +310,5 @@["help"] (put-str! help)["-h"] (put-str! help)["--help"] (put-str! help)- (/cons 'cmd _) (cmd-parse-failure (string-append "Unknown command: " cmd))+ (/cons 'cmd _) (cmd-parse-failure (error/unknown-command cmd))[] (put-str! help))))diff --git a/bin/rad-project b/bin/rad-projectindex a644431..d8789ce 100755--- a/bin/rad-project+++ b/bin/rad-project@@ -146,5 +146,5 @@["help"] (put-str! help)["-h"] (put-str! help)["--help"] (put-str! help)- (/cons 'cmd _) (parse-failure (string-append "Unknown command: " cmd) help)+ (/cons 'cmd _) (parse-failure (error/unknown-command cmd) help)[] (put-str! help))))diff --git a/rad/prelude/error-messages.rad b/rad/prelude/error-messages.radindex 5174d3e..84f9eab 100644--- a/rad/prelude/error-messages.rad+++ b/rad/prelude/error-messages.rad@@ -6,8 +6,10 @@:exports'[missing-arg too-many-args missing-arg-for-opt invalid-arg-for-optinvalid-opt-for-cmd dir-already-exists git-clone-failure upstream-commit-failure- upstream-push-failure- ]}+ upstream-push-failure item-not-found whole-item-number missing-item-number+ state-change-failure no-number-returned unknown-command unknown-commit+ checkout-new-branch-failure checkout-master-failure applying-patch-failure+ applying-accepted-patch-failure push-diff-failure missing-key-file]}(import prelude/strings :unqualified)@@ -59,6 +61,22 @@(:test "invalid-opt-for-cmd"[(invalid-opt-for-cmd "foo" "list") ==> "Invalid option \"foo\" for command list."])+;; unexported helpers for app errors++(def a-or-an+ (fn [word]+ (if (all (map (fn [x] (not (starts-with? word x))) ["a" "e" "i" "o" "u"]))+ "a"+ "an")))++(def uppercase-item+ (fn [item]+ (lookup item {:issue "Issue" :diff "Diff"})))++(def lowercase-item+ (fn [item]+ (lookup item {:issue "issue" :diff "diff"})))+;; rad project specific errors(def dir-already-exists@@ -112,3 +130,146 @@ Make sure that git was initialized correctly and rerun 'rad project init'"])(:test "upstream-push-failure"[(upstream-push-failure) ==> "Error: git -u origin master failed.Set the upstream branch manually and rerun 'rad project init'"])++;; rad diff specific errors++(def unknown-commit+ "`rad diff propose` aborts if the provided commit is unknown."+ (fn [commit]+ (string-append+ "Commit " commit " can't be found.")))++(:test "unknown-commit"+ [(unknown-commit "12345") ==> "Commit 12345 can't be found."])++(def checkout-new-branch-failure+ "`rad diff checkout` aborts if creating and switching to the diff branch fails."+ (fn [branch]+ (string-append "Creating and switching to branch " branch " failed.")))++(:test "checkout-new-branch-failure"+ [(checkout-new-branch-failure "diff/3") ==> "Creating and switching to branch diff/3 failed."])++(def checkout-master-failure+ "`rad diff accept` aborts if checking out the master branch fails."+ (fn []+ (string-append+ "Checkout branch master failed.\n"+ "Refusing to continue.")))++(:test "checkout-master-failure"+ [(checkout-master-failure) ==> "Checkout branch master failed.+Refusing to continue."])++(def applying-patch-failure+ "`rad diff checkout` aborts if applying the patch to the diff branch fails.+ Conflicts have to be resolved manually."+ (fn []+ (string-append+ "Applying the patch failed.\n"+ "Abort or resolve any conflicts via 'git am'.")))++(:test "applying-patch-failure"+ [(applying-patch-failure) ==> "Applying the patch failed.+Abort or resolve any conflicts via 'git am'."])++(def applying-accepted-patch-failure+ "`rad diff accept` aborts if applying the patch to master fails.+ Conflicts have to be resolved manually as well as pushing the commit."+ (fn []+ (string-append+ (applying-patch-failure) "\n"+ "Then run 'git push'.")))++(:test "applying-accepted-patch-failure"+ [(applying-accepted-patch-failure) ==> "Applying the patch failed.+Abort or resolve any conflicts via 'git am'.+Then run 'git push'."])++(def push-diff-failure+ "`rad diff accept` aborts if pushing the diff failed."+ (fn []+ (string-append+ "Pushing the diff failed.\n"+ "This may be because you have no internet connection, "+ "you are not authorized to push to origin, "+ "or the remote is currently not reachable.")))++(:test "push-diff-failure"+ [(push-diff-failure) ==> "Pushing the diff failed.+This may be because you have no internet connection, you are not authorized to push to origin, or the remote is currently not reachable."])++;; shared app errors++(def item-not-found+ "Any command on a specific diff/issue aborts if it does not exist."+ (fn [item item-number]+ (string-append (uppercase-item item) " #" item-number " does not exist.")))++(:test "item-not-found"+ [(item-not-found :diff "2") ==> "Diff #2 does not exist."]+ [(item-not-found :issue "3") ==> "Issue #3 does not exist."])++(def whole-item-number+ "Any command on a specific diff/issue aborts if the provided `item-number`+ is not a whole number."+ (fn [item]+ (string-append (uppercase-item item) " numbers must be whole numbers!")))++(:test "whole-item-number"+ [(whole-item-number :diff) ==> "Diff numbers must be whole numbers!"]+ [(whole-item-number :issue) ==> "Issue numbers must be whole numbers!"])++(def missing-item-number+ "Any command on a specific diff/issue aborts if the `item-number` is not provided."+ (fn [item action]+ (def item-name (lowercase-item item))+ (string-append+ "To " action " " (a-or-an item-name) " " item-name+ ", please provide the " item-name " number.")))++(:test "missing-item-number"+ [(missing-item-number :diff "change the state of") ==> "To change the state of a diff, please provide the diff number."]+ [(missing-item-number :issue "show") ==> "To show an issue, please provide the issue number."])++(def state-change-failure+ "On changing the state of a diff/issue if the daemon returned an error."+ (fn [item state]+ (string-append+ (uppercase-item item) " could not be " state ". "+ "Are you authorized to do so?")))++(:test "state-change-failure"+ [(state-change-failure :diff "rejected") ==> "Diff could not be rejected. Are you authorized to do so?"]+ [(state-change-failure :issue "closed") ==> "Issue could not be closed. Are you authorized to do so?"])++(def no-number-returned+ "On creating a diff/issue, when the creation was successful,+ but no diff/issue number was returned."+ (fn [item]+ (string-append (uppercase-item item) " created but unexpected results returned.")))++(:test "no-number-returned"+ [(no-number-returned :diff) ==> "Diff created but unexpected results returned."]+ [(no-number-returned :issue) ==> "Issue created but unexpected results returned."])++(def unknown-command+ "An unknown command for an app. E.g. `rad issue foobar`"+ (fn [cmd]+ (string-append "Unknown command: " cmd)))++(:test "unknown-command"+ [(unknown-command "foo") ==> "Unknown command: foo"])++;; key management++(def missing-key-file+ "Any request to the machine is aborted, when the key file can't be found."+ (fn []+ (string-append+ "Missing file: my-keys.rad\n"+ "Do `rad create-keys` to create your own key pair and rerun your command.")))++(:test "missing-key-file"+ [(missing-key-file) ==> "Missing file: my-keys.rad+Do `rad create-keys` to create your own key pair and rerun your command."])diff --git a/rad/prelude/machine.rad b/rad/prelude/machine.radindex 51dff17..5882033 100644--- a/rad/prelude/machine.rad+++ b/rad/prelude/machine.rad@@ -14,6 +14,9 @@(import prelude/seq :unqualified)(import prelude/key-management :as 'key-mgmt)+(file-module! "prelude/error-messages.rad")+(import prelude/error-messages :as 'error)+(def primitive-stub-ref/send! (ref daemon/send!))(def send!"Update a machine with the vector of `inputs` to evaluate. Returns a vector@@ -174,8 +177,7 @@(def get-keys!(fn [](match (key-mgmt/read-keys!)- :nothing (do (put-str! "Missing file: my-keys.rad-Do `rad-create-keys` to create your own key pair and rerun your command.")+ :nothing (do (put-str! (error/missing-key-file))(exit! 1))'keys keys)))