Skip to content

Commit

Permalink
Docs: Updated README with requirements and install/usage instructions.
Browse files Browse the repository at this point in the history
  • Loading branch information
RogWilco committed Aug 31, 2020
1 parent da42211 commit a7b8ca2
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,89 @@
# Try

[![NPM Package][npm-image]][npm-url]
[![Build Status][ci-image]][ci-url]
[![Code Coverage][coverage-image]][coverage-url]

A lightweight module that enables typed catch blocks.

## Requirements

- **[ES2015+](http://www.ecma-international.org/ecma-262/6.0/)**
- **[TypeScript 2.2+](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-2.html)**
- **[Node.js 10.0+](https://github.com/nodejs/node/blob/master/doc/changelogs/CHANGELOG_V10.md#10.0.0)**

> _Note: Please consider these caveats for [ES5](https://stackoverflow.com/questions/41102060/typescript-extending-error-class) and [TypeScript 2.1](https://github.com/Microsoft/TypeScript-wiki/blob/master/Breaking-Changes.md#extending-built-ins-like-error-array-and-map-may-no-longer-work) when using this library in an unsupported environment._
## Install

```
$ npm install @rogwilco/try
```

## Usage

Try can be used in two different styles depending on preference.

### Chaining

The chaining API completely replaces the usage of native try/catch/finally
blocks. Simply start a chain with `Try()`, providing a function for the try block,
followed by similar invocations of `Catch()` for each type of error you want to
handle, and an optinal `Finally()` block. All usages must include a trailing
call to `Done()` to ensure your code is executed.

```typescript
import Try from 'Try'

Try(() => {
// This block will be executed until an error is thrown.
throw new ReferenceError('Something went wrong!')
})
.Catch(ReferenceError, e => {
// This block is only invoked if a ReferenceError is thrown.
console.error(`Reference Error: ${e.message}`)
})
.Catch(TypeError, e => {
// This block is only invoked if a TypeError is thrown.
console.error(`Type Error: ${e.message}`)
})
.Finally(() => {
// This block is executed regardless of wheter any errors were caught.
})
.Done()
```

### Native

The native approach preserves the use of native try/catch/finally blocks, and
provides a `Handle()` function for use within the catch block. The `Handle()`
function will manage the handling of different error types (and will
automatically re-throw an unmatched error).

```typescript
import { Handle } from 'Try'

try {
// This block will be executed until an error is thrown.
throw new ReferenceError('Something went wrong!')
} catch (e) {
Handle(e, {
// This block is only invoked if a ReferenceError is thrown.
ReferenceError: e => {
console.error(`Reference Error: ${e.message}`)
},
// This block is only invoked if a TypeError is thrown.
TypeError: e => {
console.error(`Type Error: ${e.message}`)
}
})
} finally {
// This block is executed regardless of wheter any errors were caught.
}
```

[npm-image]: https://img.shields.io/npm/v/rogwilco/try?logo=npm&logoColor=white
[npm-url]: https://www.npmjs.com/package/@rogwilco/try
[ci-image]: https://img.shields.io/github/workflow/status/rogwilco/try/CI?logo=GitHub&logoColor=white
[ci-url]: https://github.com/RogWilco/try/actions?query=workflow%3ACI
[coverage-image]: https://img.shields.io/codecov/c/github/rogwilco/try?logo=codecov&logoColor=white
Expand Down

0 comments on commit a7b8ca2

Please sign in to comment.