Skip to content

Conversation

tsapeta
Copy link
Contributor

@tsapeta tsapeta commented Jan 6, 2022

Why

Since we've moved arguments validation from the module's functions to the core, the rejection errors are not always useful to narrow down the real reason of failure. One thing (probably only?) I like in Java is the exception chaining.

Also, Swift uses Error protocol to represent any kind of failures, but in some languages (including Java/Kotlin) the error means something that breaks the application that it cannot continue to run (out of memory, dividing by zero). On the other side, the exception means something that can be and should be handled by the developer.

How

  • Created base Exception class that other exceptions can inherit from
  • GenericException for parametrized exceptions
  • The exceptions can be chained using causedBy function
  • Migrated some core coded errors to exceptions
  • Added unit tests

Test Plan

Demo of console.error(err) on the JS side:
Screen Shot 2022-01-06 at 10 40 03

@expo-bot expo-bot added the bot: suggestions ExpoBot has some suggestions label Jan 6, 2022
@tsapeta tsapeta force-pushed the @tsapeta/sweet-error-chaining branch from be227e8 to 42f768f Compare January 6, 2022 10:13
@expo-bot expo-bot added bot: passed checks ExpoBot has nothing to complain about and removed bot: suggestions ExpoBot has some suggestions labels Jan 6, 2022
@tsapeta tsapeta marked this pull request as ready for review January 6, 2022 13:33
Copy link
Contributor

@Kudo Kudo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥

@tsapeta tsapeta requested review from lukmccall and bbarthec January 6, 2022 15:43
Copy link
Contributor

@bbarthec bbarthec left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it looks awesome 👍 🚀


internal class ArgumentCastException: GenericException<(index: Int, type: AnyArgumentType)> {
override var reason: String {
"Argument at index \"\(params.index)\" couldn't be casted to type \"\(params.type.description)\"."
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"Argument at index \"\(params.index)\" couldn't be casted to type \"\(params.type.description)\"."
"Argument at index '\(params.index)' couldn't be casted to type '\(params.type.description)'."

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't want to change all the quotes in this PR. I planned to do this once and separately, because there are also some errors/exceptions in the other places that I didn't touch in this PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

casted -> cast


internal class FunctionCallException: GenericException<String> {
override var reason: String {
"Call to function '\(params)' has been rejected"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"Call to function '\(params)' has been rejected"
"Call to function '\(params)' has been rejected."

/**
The location in code where the exception was created.
*/
open var location: ExceptionLocation
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm looking for the best work to describe this 👀
I know that you've discarded source, but location seems a bit odd as well (I understand location as a physical presence at some geographic area/point)

Suggested change
open var location: ExceptionLocation
open var origin: ExceptionOrigin

@tsapeta tsapeta force-pushed the @tsapeta/sweet-error-chaining branch from 63a8913 to 8e47817 Compare January 7, 2022 18:40
@tsapeta tsapeta merged commit b169f2d into master Jan 7, 2022
@tsapeta tsapeta deleted the @tsapeta/sweet-error-chaining branch January 7, 2022 21:02
lukmccall added a commit to software-mansion-labs/expo-maps that referenced this pull request Jan 27, 2022
Bump `expo-modules-core` to `0.7.0`:

# Changelog:

## 0.7.0 — 2022-01-26

### 🎉 New features

- Allow accessing `RCTBridge` from the modules on iOS. ([#15816](expo/expo#15816) by [@tsapeta](https://github.com/tsapeta))
- Added support for native callbacks through the view props in Sweet API on iOS. ([#15731](expo/expo#15731) by [@tsapeta](https://github.com/tsapeta))
- Added support for native callbacks through the view props in Sweet API on Android. ([#15743](expo/expo#15743) by [@lukmccall](https://github.com/lukmccall))
- The `ModuleDefinition` will use class name if the `name` component wasn't provided in Sweet API on Android. ([#15738](expo/expo#15738) by [@lukmccall](https://github.com/lukmccall))
- Added `onViewDestroys` component to the `ViewManager` in Sweet API on Android. ([#15740](expo/expo#15740) by [@lukmccall](https://github.com/lukmccall))
- Added shortened `constants` component that takes `vargs Pair<String, Any?>` as an argument in Sweet API on Android. ([#15742](expo/expo#15742) by [@lukmccall](https://github.com/lukmccall))
- Introduced the concept of chainable exceptions in Sweet API on iOS. ([#15813](expo/expo#15813) by [@tsapeta](https://github.com/tsapeta))
- Sweet function closures can throw errors on iOS. ([#15849](expo/expo#15849) by [@tsapeta](https://github.com/tsapeta))
- Add `requireNativeModule` function to replace accessing native modules from `NativeModulesProxy`. ([#15848](expo/expo#15848) by [@tsapeta](https://github.com/tsapeta))
- Implemented basic functionality of JSI host object to replace `NativeModulesProxy` on iOS. ([#15847](expo/expo#15847) by [@tsapeta](https://github.com/tsapeta))

### 🐛 Bug fixes

- It's no longer possible to directly call methods from the `ModuleDefinition` in the `ViewManagers` on Android. ([#15741](expo/expo#15741) by [@lukmccall](https://github.com/lukmccall))
- Fix compatibility with react-native 0.66. ([#15914](expo/expo#15914) by [@Kudo](https://github.com/kudo))

## 0.6.4 — 2022-01-05

### 🐛 Bug fixes

- Fix `ReactInstanceManager.onHostPause` exception from moving Android apps to background. ([#15748](expo/expo#15748) by [@Kudo](https://github.com/kudo))

## 0.6.3 — 2021-12-16

### 🐛 Bug fixes

- Fixed the deep link wasn't passed to the application if the application wasn't running when the deep link was sent. ([#15593](expo/expo#15593) by [@lukmccall](https://github.com/lukmccall))

## 0.6.2 — 2021-12-15

### 🎉 New features

- Add `onNewIntent` and `onBackPressed` support to `ReactActivityLifecycleListener`. ([#15550](expo/expo#15550) by [@Kudo](https://github.com/Kudo))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bot: passed checks ExpoBot has nothing to complain about
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants