Skip to content

Commit

Permalink
docs: add error handling practices
Browse files Browse the repository at this point in the history
  • Loading branch information
alestiago committed Sep 3, 2024
1 parent d4020c4 commit 64b1002
Showing 1 changed file with 105 additions and 0 deletions.
105 changes: 105 additions & 0 deletions src/content/docs/error_handling/error_handling.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
---
title: Error handling
description: Error handling best practices.
---

## Document when a call may throw

Inform about the potential risks associated with calling a function, helping understand under what conditions an exception might be thrown.

This transparency allows developers to handle exceptions properly, leading to more robust and error-resistant code. Thus, reducing the likelihood of unintended errors.

<Tabs>
<TabItem label="Good ✅">

```dart
/// Deletes permanenetly an account with the given [name].

Check warning on line 16 in src/content/docs/error_handling/error_handling.md

View workflow job for this annotation

GitHub Actions / build / build

Unknown word (permanenetly)
///
/// Throws:
///
/// * [UnothorizedException] if the active role is not [Role.admin], since only

Check warning on line 20 in src/content/docs/error_handling/error_handling.md

View workflow job for this annotation

GitHub Actions / build / build

Unknown word (Unothorized)
/// admins are authorized to delete accounts.
void deleteAccount(String name) {
if (activeRole != Role.admin) {
throw UnothorizedException('Only admin can delete account');

Check warning on line 24 in src/content/docs/error_handling/error_handling.md

View workflow job for this annotation

GitHub Actions / build / build

Unknown word (Unothorized)
}
// ...
}
```

</TabItem>
<TabItem label="Bad ❗️">

```dart
/// Deletes permanenetly an account with the given [name].

Check warning on line 34 in src/content/docs/error_handling/error_handling.md

View workflow job for this annotation

GitHub Actions / build / build

Unknown word (permanenetly)
void deleteAccount(String name) {
if (activeRole != Role.admin) {
throw UnothorizedException('Only admin can delete account');

Check warning on line 37 in src/content/docs/error_handling/error_handling.md

View workflow job for this annotation

GitHub Actions / build / build

Unknown word (Unothorized)
}
// ...
}
```

</TabItem>
</Tabs>

## Define implemented exceptions

Implement `Exception` with descriptive names rather than simply throwing a generic `Exception`.

By creating custom exceptions, developers can provide more meaningful error messages and handle different error types in a more granular way. This enhances code readability and maintainability, as it becomes clear what type of error is being dealt with.

<Tabs>
<TabItem label="Good ✅">

```dart
class UnothorizedException implements Exception {

Check warning on line 56 in src/content/docs/error_handling/error_handling.md

View workflow job for this annotation

GitHub Actions / build / build

Unknown word (Unothorized)
UnothorizedException(this.message);

Check warning on line 57 in src/content/docs/error_handling/error_handling.md

View workflow job for this annotation

GitHub Actions / build / build

Unknown word (Unothorized)
final String message;
@override
String toString() => 'UnothorizedException: $message';
}
void deleteAccount(String name) {
if (activeRole != Role.admin) {
throw UnothorizedException('Only admin can delete account');
}
// ...
}
void main() {
try {
deleteAccount('user');
} on UnothorizedException catch (e) {
// Handle the exception.
}
}
```

</TabItem>
<TabItem label="Bad ❗️">

```dart
void deleteAccount(String name) {
if (activeRole != Role.admin) {
throw Exception('Only admin can delete account');
}
// ...
}
void main() {
try {
deleteAccount('user');
} on Exception catch (e) {
// Exception is a marker interface implemented by all core library exceptions,
// it is very generic and it could be catching many different types of exceptions,
// lacking intent and making the code harder to understand.
}
}
```

</TabItem>
</Tabs>

0 comments on commit 64b1002

Please sign in to comment.