Skip to content

Commit

Permalink
fix: export errors
Browse files Browse the repository at this point in the history
  • Loading branch information
dnlup committed Mar 17, 2021
1 parent 0633fed commit e0d49a2
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 13 deletions.
22 changes: 16 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ Its API lets you access both computed and raw values, where possible.
* [`doc.eventLoopUtilizationSupported`](#doceventlooputilizationsupported)
* [`doc.resourceUsageSupported`](#docresourceusagesupported)
* [`doc.gcFlagsSupported`](#docgcflagssupported)
* [`doc.errors`](#docerrors)

<!-- tocstop -->

Expand Down Expand Up @@ -393,31 +394,31 @@ It tracks the global activity of the garbage collector.

* [`<GCEntry>`](#class-gcentry) | [`<GCAggregatedEntry>`](#class-gcaggregatedentry)

The activity of the operation of type `major`. It's present only if `GCMEtric` has been created with the option `aggregate` equal to `true`.
The activity of the operation of type `major`. It's present only if `GCMetric` has been created with the option `aggregate` equal to `true`.

See [`performanceEntry.kind`](https://nodejs.org/dist/latest-v12.x/docs/api/perf_hooks.html#perf_hooks_performanceentry_kind).

#### `gcMetric.minor`

* [`<GCEntry>`](#class-gcentry) | [`<GCAggregatedEntry>`](#class-gcaggregatedentry)

The activity of the operation of type `minor`. It's present only if `GCMEtric` has been created with the option `aggregate` equal to `true`.
The activity of the operation of type `minor`. It's present only if `GCMetric` has been created with the option `aggregate` equal to `true`.

See [`performanceEntry.kind`](https://nodejs.org/dist/latest-v12.x/docs/api/perf_hooks.html#perf_hooks_performanceentry_kind).

#### `gcMetric.incremental`

* [`<GCEntry>`](#class-gcentry) | [`<GCAggregatedEntry>`](#class-gcaggregatedentry)

The activity of the operation of type `incremental`. It's present only if `GCMEtric` has been created with the option `aggregate` equal to `true`.
The activity of the operation of type `incremental`. It's present only if `GCMetric` has been created with the option `aggregate` equal to `true`.

See [`performanceEntry.kind`](https://nodejs.org/dist/latest-v12.x/docs/api/perf_hooks.html#perf_hooks_performanceentry_kind).

#### `gcMetric.weakCb`

* [`<GCEntry>`](#class-gcentry) | [`<GCAggregatedEntry>`](#class-gcaggregatedentry)

The activity of the operation of type `weakCb`. It's present only if `GCMEtric` has been created with the option `aggregate` equal to `true`.
The activity of the operation of type `weakCb`. It's present only if `GCMetric` has been created with the option `aggregate` equal to `true`.

See [`performanceEntry.kind`](https://nodejs.org/dist/latest-v12.x/docs/api/perf_hooks.html#perf_hooks_performanceentry_kind).

Expand All @@ -427,7 +428,7 @@ It contains garbage collection data, represented with an [hdr histogram](https:/

#### `new GCEntry()`

The initialization doesn't require options. It is created internally by a [`GCMEtric`](#class-gcmetric).
The initialization doesn't require options. It is created internally by a [`GCMetric`](#class-gcmetric).

#### `gcEntry.totalDuration`

Expand Down Expand Up @@ -482,7 +483,7 @@ It extends [`GCEntry`](#class-gcentry) and contains garbage collection data plus

#### `new GCAggregatedEntry()`

The initialization doesn't require options. It is created internally by a [`GCMEtric`](#class-gcmetric).
The initialization doesn't require options. It is created internally by a [`GCMetric`](#class-gcmetric).

#### `gcAggregatedEntry.flags`

Expand Down Expand Up @@ -538,3 +539,12 @@ It tells if the Node.js version in use supports the [resourceUsage metric](https
* `<boolean>`

It tells if the Node.js version in use supports [GC flags](https://nodejs.org/dist/latest-v12.x/docs/api/perf_hooks.html#perf_hooks_performanceentry_flags).

### `doc.errors`

In the `errors` object are exported all the custom errors used by the module.

| Error | Error Code | Description |
|-------|------------|-------------|
| `InvalidArgumentError` | `DOC_ERR_INVALID_ARG` | An invalid option or argument was used |
| `NotSupportedError` | `DOC_ERR_NOT_SUPPORTED` | A metric is not supported on the Node.js version used |
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Sampler, SamplerOptions } from './types/sampler'
import errors = require('./types/errors')

declare function createSampler(options?: SamplerOptions): Sampler
export default createSampler
Expand All @@ -8,6 +9,7 @@ export const eventLoopUtilizationSupported: Boolean
export const resourceUsageSupported: Boolean
export const gcFlagsSupported: Boolean
export { Sampler, SamplerOptions }
export { errors }
export * from './types/cpuMetric'
export * from './types/eventLoopDelayMetric'
export * from './types/resourceUsageMetric'
Expand Down
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const Sampler = require('./lib/sampler')
const util = require('./lib/util')
const errors = require('./lib/errors')

function createSampler (options = {}) {
return new Sampler(options)
Expand All @@ -12,6 +13,7 @@ module.exports.doc = createSampler
module.exports.default = createSampler

module.exports.Sampler = Sampler
module.exports.errors = errors

Object.defineProperty(module.exports, 'eventLoopUtilizationSupported', {
value: util.eventLoopUtilizationSupported,
Expand Down
19 changes: 13 additions & 6 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
import { expectType, expectError } from 'tsd'
import { expectType, expectError, expectAssignable } from 'tsd'
import {
Sampler,
createSampler,
eventLoopUtilizationSupported,
resourceUsageSupported,
gcFlagsSupported,
Sampler,
errors,
CPUMetric,
ResourceUsageMetric,
EventLoopDelayMetric,
EventLoopUtilizationMetric,
GCMetric,
eventLoopUtilizationSupported,
resourceUsageSupported,
gcFlagsSupported
GCMetric
} from '.'

expectType<Boolean>(eventLoopUtilizationSupported)
expectType<Boolean>(resourceUsageSupported)
expectType<Boolean>(gcFlagsSupported)
expectAssignable<errors.NotSupportedError>(new errors.NotSupportedError())
expectAssignable<'NotSupportedError'>(new errors.NotSupportedError().name)
expectAssignable<'DOC_ERR_NOT_SUPPORTED'>(new errors.NotSupportedError().code)
expectAssignable<errors.InvalidArgumentError>(new errors.InvalidArgumentError())
expectAssignable<'InvalidArgumentError'>(new errors.InvalidArgumentError().name)
expectAssignable<'DOC_ERR_INVALID_ARG'>(new errors.InvalidArgumentError().code)

let sampler: Sampler

Expand Down
3 changes: 2 additions & 1 deletion test/esm/namedExports.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { strictEqual } from 'assert'
import { doc, Sampler, eventLoopUtilizationSupported, resourceUsageSupported, gcFlagsSupported } from '../../index.js'
import { doc, Sampler, errors, eventLoopUtilizationSupported, resourceUsageSupported, gcFlagsSupported } from '../../index.js'

strictEqual(typeof doc, 'function')
strictEqual(typeof Sampler, 'function')
strictEqual(typeof errors, 'object')
strictEqual(typeof eventLoopUtilizationSupported, 'boolean')
strictEqual(typeof resourceUsageSupported, 'boolean')
strictEqual(typeof gcFlagsSupported, 'boolean')
15 changes: 15 additions & 0 deletions types/errors.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
declare namespace Errors {
/** An argument or option was invalid */
export class InvalidArgumentError extends Error {
name: 'InvalidArgumentError'
code: 'DOC_ERR_INVALID_ARG'
}

/** A metric is not supported */
export class NotSupportedError extends Error {
name: 'NotSupportedError'
code: 'DOC_ERR_NOT_SUPPORTED'
}
}

export = Errors

0 comments on commit e0d49a2

Please sign in to comment.