Skip to content

Commit

Permalink
refactor: Rename ErrDisplayFormat and update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmah309 committed Nov 25, 2024
1 parent cd993c2 commit 6d7855f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 47 deletions.
40 changes: 20 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void main() {
}
Result<String> order(String user, int orderNumber) {
final result = makeFood(orderNumber).context("Could not order for user: $user.");
final result = makeFood(orderNumber).context("Could not order for $user.");
if(result case Ok(:final ok)) {
return Ok("Order of $ok is complete for $user");
}
Expand All @@ -42,22 +42,22 @@ Result<String> order(String user, int orderNumber) {
Result<String> makeFood(int orderNumber) {
if (orderNumber == 1) {
return makeHamburger().context("Order number $orderNumber failed.");
return makePizza().context("Order was number $orderNumber.");
}
return Ok("pasta");
}
Result<String> makeHamburger() {
return bail("Hmm something went wrong making the hamburger.");
Result<String> makePizza() {
return bail("Pizza was missing a topping.");
}
```
#### Output
```text
Error: Could not order for user: Bob.
Error: Could not order for Bob.
Caused by:
0: Order number 1 failed.
1: Hmm something went wrong making the hamburger.
0: Order was number 1.
1: Pizza was missing a topping..
StackTrace:
#0 AnyhowResultExtensions.context (package:anyhow/src/anyhow/anyhow_extensions.dart:12:29)
Expand All @@ -80,24 +80,24 @@ Result<String, String> order(String user, int orderNumber) {
if(result case Ok(:final ok)) {
return Ok("Order of $ok is complete for $user");
}
Logging.w("Could not order for user: $user.");
Logging.w("Could not order for $user.");
return result;
}
Result<String, String> makeFood(int orderNumber) {
if (orderNumber == 1) {
final result = makeHamburger();
final result = makePizza();
if (result.isErr()) {
Logging.w("Order number $orderNumber failed.");
Logging.w("Order was number $orderNumber.");
}
return result;
}
return Ok("pasta");
}
Result<String, String> makeHamburger() {
Result<String, String> makePizza() {
// What is the context around this error??
return Err("Hmm something went wrong making the hamburger.");
return Err("Pizza was missing a topping.");
}
```

Expand Down Expand Up @@ -127,25 +127,25 @@ Which is usually done at startup.
* `hasStackTrace`: With `Error.hasStackTrace = false`, we can exclude capturing a stack trace:

```text
Error: Could not order for user: Bob.
Error: Could not order for Bob.
Caused by:
0: Order number 1 failed.
1: Hmm something went wrong making the hamburger.
0: Order was number 1.
1: Pizza was missing a topping.
```

* `displayFormat`: We can view the root cause first with `Error.displayFormat = ErrDisplayFormat.rootCauseFirst`
* `displayFormat`: We can view the root cause first with `Error.displayFormat = ErrorDisplayFormat.rootCauseFirst`

```text
Root Cause: Hmm something went wrong making the hamburger.
Root Cause: Pizza was missing a topping.
Additional Context:
0: Order number 1 failed.
1: Could not order for user: Bob.
0: Order was number 1.
1: Could not order for Bob.
StackTrace:
#0 bail (package:anyhow/src/anyhow/functions.dart:6:14)
#1 makeHamburger (package:anyhow/test/src/temp.dart:31:10)
#1 makePizza (package:anyhow/test/src/temp.dart:31:10)
... <OMITTED FOR EXAMPLE>
```

Expand Down
22 changes: 11 additions & 11 deletions lib/src/error.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Error implements Exception {
static bool hasStackTrace = true;

/// Setting for how errors are converted to strings
static ErrDisplayFormat displayFormat = ErrDisplayFormat.traditionalAnyhow;
static ErrorDisplayFormat displayFormat = ErrorDisplayFormat.rootCauseLast;

/// How to display [StackTrace]s. Requires [hasStackTrace] = true
static StackTraceDisplayFormat stackTraceDisplayFormat =
Expand Down Expand Up @@ -91,12 +91,12 @@ class Error implements Exception {
String toString() {
final StringBuffer stringBuf = StringBuffer();
switch (displayFormat) {
case ErrDisplayFormat.traditionalAnyhow:
case ErrorDisplayFormat.rootCauseLast:
final list = chain();
_writeErrorAndContext(stringBuf, "Error", "Caused by", list.iterator);
_writeStackTraces(stringBuf, list.iterator);
break;
case ErrDisplayFormat.rootCauseFirst:
case ErrorDisplayFormat.rootCauseFirst:
final list = chain().toList(growable: false).reversed;
_writeErrorAndContext(
stringBuf, "Root Cause", "Additional Context", list.iterator);
Expand Down Expand Up @@ -164,20 +164,20 @@ class Error implements Exception {
}

/// Controls the base [toString] format
enum ErrDisplayFormat {
/// Top level to low level. Ex:
enum ErrorDisplayFormat {
/// Traditional anyhow display. The most recent context to the root cause. E.g.:
/// Error: Bob ordered.
///
/// Caused by:
/// 0: order was pizza.
/// 1: Hmm something went wrong making the hamburger.
traditionalAnyhow,
/// 0: Order was pizza.
/// 1: Pizza was missing a topping.
rootCauseLast,

/// Root cause to additional context added above. Ex:
/// Root Cause: Hmm something went wrong making the hamburger.
/// Root cause to additional context added above. E.g.:
/// Root Cause: Pizza was missing a topping.
///
/// Additional Context:
/// 0: order was pizza.
/// 0: Order was pizza.
/// 1: Bob ordered.
rootCauseFirst
}
Expand Down
32 changes: 16 additions & 16 deletions test/result_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -427,12 +427,12 @@ void main() {
test('printing error', () {
Error.hasStackTrace = false;
Result error = bail(Exception("Root cause"));
Error.displayFormat = ErrDisplayFormat.traditionalAnyhow;
Error.displayFormat = ErrorDisplayFormat.rootCauseLast;
expect(error.toString(), 'Error: Exception: Root cause\n');
Error.displayFormat = ErrDisplayFormat.rootCauseFirst;
Error.displayFormat = ErrorDisplayFormat.rootCauseFirst;
expect(error.toString(), 'Root Cause: Exception: Root cause\n');
error = order();
Error.displayFormat = ErrDisplayFormat.traditionalAnyhow;
Error.displayFormat = ErrorDisplayFormat.rootCauseLast;
//print(error.toString());
expect(error.toString(), """
Error: Bob ordered.
Expand All @@ -441,7 +441,7 @@ Caused by:
0: order was pizza.
1: Hmm something went wrong making the hamburger.
""");
Error.displayFormat = ErrDisplayFormat.rootCauseFirst;
Error.displayFormat = ErrorDisplayFormat.rootCauseFirst;
// print(error.toString());
expect(error.toString(), """
Root Cause: Hmm something went wrong making the hamburger.
Expand All @@ -456,12 +456,12 @@ Additional Context:
Error.hasStackTrace = true;
Error.stackTraceDisplayFormat = StackTraceDisplayFormat.none;
Result error = bail(Exception("Root cause"));
Error.displayFormat = ErrDisplayFormat.traditionalAnyhow;
Error.displayFormat = ErrorDisplayFormat.rootCauseLast;
expect(error.toString(), 'Error: Exception: Root cause\n');
Error.displayFormat = ErrDisplayFormat.rootCauseFirst;
Error.displayFormat = ErrorDisplayFormat.rootCauseFirst;
expect(error.toString(), 'Root Cause: Exception: Root cause\n');
error = order();
Error.displayFormat = ErrDisplayFormat.traditionalAnyhow;
Error.displayFormat = ErrorDisplayFormat.rootCauseLast;
//print(error.toString());
expect(error.toString(), """
Error: Bob ordered.
Expand All @@ -470,7 +470,7 @@ Caused by:
0: order was pizza.
1: Hmm something went wrong making the hamburger.
""");
Error.displayFormat = ErrDisplayFormat.rootCauseFirst;
Error.displayFormat = ErrorDisplayFormat.rootCauseFirst;
// print(error.toString());
expect(error.toString(), """
Root Cause: Hmm something went wrong making the hamburger.
Expand All @@ -482,20 +482,20 @@ Additional Context:

Error.stackTraceDisplayFormat = StackTraceDisplayFormat.one;
error = bail(Exception("Root cause"));
Error.displayFormat = ErrDisplayFormat.traditionalAnyhow;
Error.displayFormat = ErrorDisplayFormat.rootCauseLast;
expect(error.toString(), startsWith("""
Error: Exception: Root cause
StackTrace:
"""));
Error.displayFormat = ErrDisplayFormat.rootCauseFirst;
Error.displayFormat = ErrorDisplayFormat.rootCauseFirst;
expect(error.toString(), startsWith("""
Root Cause: Exception: Root cause
StackTrace:
"""));
error = order();
Error.displayFormat = ErrDisplayFormat.traditionalAnyhow;
Error.displayFormat = ErrorDisplayFormat.rootCauseLast;
//print(error.toString());
expect(error.toString(), startsWith("""
Error: Bob ordered.
Expand All @@ -506,7 +506,7 @@ Caused by:
StackTrace:
"""));
Error.displayFormat = ErrDisplayFormat.rootCauseFirst;
Error.displayFormat = ErrorDisplayFormat.rootCauseFirst;
// print(error.toString());
expect(error.toString(), startsWith("""
Root Cause: Hmm something went wrong making the hamburger.
Expand All @@ -520,22 +520,22 @@ StackTrace:

Error.stackTraceDisplayFormat = StackTraceDisplayFormat.full;
error = bail(Exception("Root cause"));
Error.displayFormat = ErrDisplayFormat.traditionalAnyhow;
Error.displayFormat = ErrorDisplayFormat.rootCauseLast;
expect(error.toString(), startsWith("""
Error: Exception: Root cause
Main StackTrace:
"""));
expect(error.toString(), isNot(contains("Additional StackTraces")));
Error.displayFormat = ErrDisplayFormat.rootCauseFirst;
Error.displayFormat = ErrorDisplayFormat.rootCauseFirst;
expect(error.toString(), startsWith("""
Root Cause: Exception: Root cause
Main StackTrace:
"""));
expect(error.toString(), isNot(contains("Additional StackTraces")));
error = order();
Error.displayFormat = ErrDisplayFormat.traditionalAnyhow;
Error.displayFormat = ErrorDisplayFormat.rootCauseLast;
//print(error.toString());
expect(error.toString(), startsWith("""
Error: Bob ordered.
Expand All @@ -547,7 +547,7 @@ Caused by:
Main StackTrace:
"""));
expect(error.toString(), contains("Additional StackTraces"));
Error.displayFormat = ErrDisplayFormat.rootCauseFirst;
Error.displayFormat = ErrorDisplayFormat.rootCauseFirst;
// print(error.toString());
expect(error.toString(), startsWith("""
Root Cause: Hmm something went wrong making the hamburger.
Expand Down

0 comments on commit 6d7855f

Please sign in to comment.