Automattic / Automattic/wordpress-rs

`enum` fallback design allows for silent API breakages

Open
#268 0 comments 0 reactions 1 assignee Claimed by @oguzkocer View on GitHub
Error Handling
Dominant language
Rust
Stars
36
Forks
5
Avg merge
17h 30m
Merged PRs (30d)
43

Description

Consider the `WpErrorCode` implementation below:

```
fn showcase(error_code: WpErrorCode) {
if let WpErrorCode::CustomError(error_code_as_string) = error_code {
if error_code_as_string == "foo" {
// handle the "foo" error
}
}
```

If we were to add a new `WpErrorCode::Foo` variant, it'll start to fail and has to be handled as such:

```
fn showcase(error_code: WpErrorCode) {
if let WpErrorCode::Foo = error_code {
// handle the "foo" error
}
}
```

This kind of silent API breakages are very dangerous. Our design should allow us to add more error variants without breaking the existing implementation.

---

The same issue can be seen in other `enum` fallbacks as well. For example, if we were to add a new variant to `PostTypeCapabilities`, it can break the same way.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.