Skip to content

Commit

Permalink
Remove custom error usage
Browse files Browse the repository at this point in the history
  • Loading branch information
greguz committed Jul 9, 2020
1 parent 7be4428 commit 6726c76
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 75 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Make fluent objects like a boss!

## Core features

- **Zero dependencies**: small footprint.
- **Protected state**
- **Undo/Redo out of the box**
- **Selective mutability**: choose between immutable objects or a classy object.
Expand Down Expand Up @@ -377,15 +378,14 @@ const calculator = createBoundCalculator(0)
console.log(calculator.unwrap()) // Logs '42'
```

By default, Fluente still protects the coder from an erroneous unbound call, by throwing a particular error.
By default, Fluente will throw an 'Unbound call' error when necessary.

```javascript
const calculator = createNormalCalculator(0)

try {
calculator.add.call(null, 1)
} catch (err) {
console.log(err.message) // Logs 'Unbound call'
console.log(err.code) // Logs 'FLUENTE_UNBOUND'
console.log(err) // Error: Unbound call
}
```
4 changes: 1 addition & 3 deletions fluente.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
'use strict'

const Herry = require('herry')

const stateSymbol = Symbol('fluente')

function getState (obj) {
if (typeof obj === 'object' && obj !== null && obj.hasOwnProperty(stateSymbol)) {
return obj[stateSymbol]
} else {
throw new Herry('FLUENTE_UNBOUND', 'Unbound call')
throw new Error('Unbound call')
}
}

Expand Down
8 changes: 4 additions & 4 deletions fluente.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,11 +252,11 @@ test('binding', t => {
t.true(a[sym])
t.throws(
() => a.next.call(null),
{ code: 'FLUENTE_UNBOUND' }
{ message: 'Unbound call' }
)
t.throws(
() => a.unwrap.call(null),
{ code: 'FLUENTE_UNBOUND' }
{ message: 'Unbound call' }
)

let b = fluente({
Expand Down Expand Up @@ -294,10 +294,10 @@ test('binding', t => {
t.true(c[sym])
t.throws(
() => c.next.call(null),
{ code: 'FLUENTE_UNBOUND' }
{ message: 'Unbound call' }
)
t.throws(
() => c.unwrap.call(null),
{ code: 'FLUENTE_UNBOUND' }
{ message: 'Unbound call' }
)
})

0 comments on commit 6726c76

Please sign in to comment.