Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions docs/WorkflowGuidelines.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,47 @@
```

This way, the code is more performant (even though this is the least important aspect), readable (as we can immediately see what is the potentially problematic variable) and maintainable (as new unexpected but unrelated exceptions can be generated by future versions of the library that contains the function called).

* Avoid applying logic based on exception's message:

Exception messages (even in stable libraries/softwares) are subject to change, applying logic based on the `Message` or `StackTrace` can cause hard-to-debug problems when updating your dependencies or .NET version.

Example:
```
try
let userObj = ctx.Public.Users.Create()
userObj.Id <- 1
userObj.Name <- "Alice"
ctx.SubmitUpdates()
()
with
| :? PostgresException as ex when
ex.MessageText.Contains "duplicate key value" ->
Console.WriteLine "Primary key violation was detected when adding a new user:"
Console.WriteLine (ex.ToString())
()
```

Improved code:
```
// Returns true if exception indicates uniqueness problem
let isDuplicatePrimaryKeyError(ex: PostgresException) =
let uniqueViolationErrorCode = "23505"
ex.SqlState = uniqueViolationErrorCode

try
let userObj = ctx.Public.Users.Create()
userObj.Id <- 1
userObj.Name <- "Alice"
ctx.SubmitUpdates()
()
with
| :? PostgresException as ex when
isDuplicatePrimaryKeyError ex ->
Console.WriteLine "Primary key violation was detected when adding a new user:"
Console.WriteLine (ex.ToString())
()
```

* Not benefiting from your type system:

Expand Down