Skip to content

Commit

Permalink
Merge pull request scribble#102 from nickng/error-handling-patch
Browse files Browse the repository at this point in the history
Error handling patch
  • Loading branch information
rhu1 authored Oct 12, 2018
2 parents 3cb00ef + 062522a commit 270f060
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,24 @@ public String buildArgs(STStateChanApiBuilder api, EAction a)
}
}

/**
* checkError helper function surrounds the given expression expr with Go's error check
* and assigns errorField with err if it exists, i.e.
*
* <pre>{@code
* if err := $expression; err != nil {
* $errField = err
* }
* }</pre>
*
* @param expr Expression that returns error
* @param errField Location to store the error
* @return code fragment of expr with error check
*/
private String checkError(String expr, String errField) {
return "if err := " + expr + "; err != nil {\n" + errField + " = err\n" + "}\n";
}

@Override
public String buildBody(STStateChanApiBuilder api, EState curr, EAction a, EState succ)
{
Expand Down Expand Up @@ -131,23 +149,21 @@ public String buildBody(STStateChanApiBuilder api, EState curr, EAction a, EStat
throw new RuntimeException("[rp-core] [param-api] TODO: " + a);
}

res += errorField + " = "
+ sEpWrite
res += checkError(sEpWrite
+ "."
+ (a.mid.isOp() ? RPCoreSTApiGenConstants.GO_MPCHAN_ISEND : RPCoreSTApiGenConstants.GO_MPCHAN_MSEND)
+ "(\"" + r.getName() + "\", i"
+ IntStream.range(0, a.payload.elems.size()).mapToObj(i -> ", &arg" + i + "[j]").collect(Collectors.joining(""))
+ ")\n";
+ ")", errorField);
}
}
else //(a.mid.isMessageSigName)
{
// FIXME: factor out with above
res += errorField + " = "
+ sEpWrite
res += checkError( sEpWrite
+ "."
+ (a.mid.isOp() ? RPCoreSTApiGenConstants.GO_MPCHAN_ISEND : RPCoreSTApiGenConstants.GO_MPCHAN_MSEND)
+ "(\"" + r.getName() + "\", i, &arg0[j])\n";
+ "(\"" + r.getName() + "\", i, &arg0[j])", errorField);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,11 @@ public int compare(Entry<Integer, String> o1, Entry<Integer, String> o2)
// cf. state chan builder // FIXME: chan struct reuse
//"ini._" + init + ".id = 1\n" +
"return f(ini._" + init + ")\n")*/
+ "return f(ini.Init())\n"
+ "end := f(ini.Init())\n"
+ "if end." + RPCoreSTApiGenConstants.GO_MPCHAN_ERR + " != nil {\n"
+ "panic(end." + RPCoreSTApiGenConstants.GO_MPCHAN_ERR + ")\n"
+ "}\n"
+ "return end\n"

+ "}";

Expand Down

0 comments on commit 270f060

Please sign in to comment.