Skip to content

Commit

Permalink
change error class and document it
Browse files Browse the repository at this point in the history
  • Loading branch information
regevbr committed Jun 1, 2019
1 parent 56e48e2 commit c079333
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 21 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ This project adheres to [Semantic Versioning](http://semver.org/).
### Added

- allow unique id enforcement on event creation
- Error handling docs

### Fixed

- Changed error class name to a more suitable one

## v0.1.0

Expand Down
21 changes: 18 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ If you want to use the timing utils, you must also install `moment`

```typescript
import { CronicleClient, NumberedBoolean, BaseCategories, BaseTargets, getUtcTiming,
HttpPluginMethods, basePlugins} from 'cronicle-client';
HttpPluginMethods, basePlugins, CronicleError} from 'cronicle-client';

const scheduler = new CronicleClient({
masterUrl: 'http://localhost:3012',
Expand Down Expand Up @@ -65,14 +65,17 @@ scheduler.createEvent({
})
.then((data) => {
console.log(`Started event with job id: ${data.ids[0]}`);
})
.catch((err: CronicleError) => {
console.log(`Cronicle error: ${err.code} - ${err.message}`);
});
```

## Extending with custom types example

```typescript
import { CronicleClient, IHttpPluginData, IShellPluginData, ITestPluginData, NumberedBoolean,
getUtcTiming, IPluginNames } from 'cronicle-client';
getUtcTiming, IPluginNames, CronicleError } from 'cronicle-client';

interface ICustomPluginData {
duration: string;
Expand Down Expand Up @@ -136,14 +139,19 @@ scheduler.createEvent({
})
.then((data) => {
console.log(`Started event with job id: ${data.ids[0]}`);
})
.catch((err: CronicleError) => {
console.log(`Cronicle error: ${err.code} - ${err.message}`);
});
```

## Documentation

### Methods

For all api endpoints documentations, please refer to [Cronicle api reference](https://github.com/jhuckaby/Cronicle#api-reference)

### createEvent
#### createEvent

When creating an event, there is no unique restriction on the title/id.
While searching for an event using `getEvent`, the
Expand All @@ -152,6 +160,13 @@ This imposes an issue when you don't enforce a unique title/id since you will ge
Until this behaviour is fixed, you can tell the `createEvent` method to enforce uniqueness and it will fail if an event with the provided title/id already exists.
Note: if `id` is provided - it will be used as the unique key, otherwise `title` will be used.

### Error handling

`Croncile` will always return a valid HTTP response (code `200`).
To raise an error, the `code` property of the response will be different than `0`.
In such cases, the current method will be rejected with `CronicleError` with the proper error message and the
`code` property.

### Constructor

#### Options
Expand Down
6 changes: 3 additions & 3 deletions src/CronicleClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import * as qs from 'querystring';
import * as request from 'request-promise';
import {SchedulerError} from './SchedulerError';
import {CronicleError} from './CronicleError';
import {
IAbortJobRequest,
IBasicResponse,
Expand Down Expand Up @@ -81,7 +81,7 @@ export class CronicleClient<Categories extends string = BaseCategories,
})
.then((resp) => {
if (resp) {
return Promise.reject(new SchedulerError({
return Promise.reject(new CronicleError({
code: 'unique',
description: 'event already exists',
}));
Expand Down Expand Up @@ -135,7 +135,7 @@ export class CronicleClient<Categories extends string = BaseCategories,
return Promise.resolve(request(this._buildRequest(op, method, bodyOrQuery)))
.then((response: T | IErrorResponse) => {
if (response.code !== 0) {
return Promise.reject(new SchedulerError(response as IErrorResponse));
return Promise.reject(new CronicleError(response as IErrorResponse));
}
return Promise.resolve(response as T);
});
Expand Down
2 changes: 1 addition & 1 deletion src/SchedulerError.ts → src/CronicleError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { IErrorResponse } from './requestResponseTypes';

export class SchedulerError extends Error {
export class CronicleError extends Error {
public code: 0 | number | string;

constructor(resp: IErrorResponse) {
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ export * from './requestResponseTypes';
export * from './dataTypes';
export * from './helperTypes';
export * from './plugins';
export * from './SchedulerError';
export * from './CronicleError';
export * from './timingUtils';
26 changes: 13 additions & 13 deletions test/src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
getUtcTiming,
getTiming,
CronicleClient,
SchedulerError,
CronicleError,
NumberedBoolean,
BaseCategories,
BaseTargets,
Expand All @@ -27,7 +27,7 @@ const stubs = {
// tslint:disable-next-line
const allImports = proxyquire('../../dist/', stubs);
const cronicleClientStubbed: typeof CronicleClient = allImports.CronicleClient;
const cchedulerErrorStubbed: typeof SchedulerError = allImports.SchedulerError;
const cronicleErrorStubbed: typeof CronicleError = allImports.CronicleError;

const masterUrl = 'http://my.croncicle.com:3012';
const apiKey = 'myApiKey';
Expand Down Expand Up @@ -327,7 +327,7 @@ describe('cronicle client', () => {
requestStub.resolves({code, description});
client.getEvent({id})
.catch((error) => {
error.should.be.instanceOf(cchedulerErrorStubbed);
error.should.be.instanceOf(cronicleErrorStubbed);
expect(error.code).to.eql(code);
expect(error.message).to.eql(description);
done();
Expand Down Expand Up @@ -449,7 +449,7 @@ limit=${limit}&offset=${offset}`,
requestStub.resolves({code, description});
client.getSchedule()
.catch((error) => {
error.should.be.instanceOf(cchedulerErrorStubbed);
error.should.be.instanceOf(cronicleErrorStubbed);
expect(error.code).to.eql(code);
expect(error.message).to.eql(description);
done();
Expand Down Expand Up @@ -536,7 +536,7 @@ limit=${limit}&offset=${offset}`,
requestStub.resolves({code, description});
client.runEvent({id})
.catch((error) => {
error.should.be.instanceOf(cchedulerErrorStubbed);
error.should.be.instanceOf(cronicleErrorStubbed);
expect(error.code).to.eql(code);
expect(error.message).to.eql(description);
done();
Expand Down Expand Up @@ -668,7 +668,7 @@ limit=${limit}&offset=${offset}`,
};
client.createEvent(request, true)
.catch((error) => {
error.should.be.instanceOf(cchedulerErrorStubbed);
error.should.be.instanceOf(cronicleErrorStubbed);
expect(error.code).to.eql('unique');
expect(error.message).to.eql('event already exists');
expect(requestStub.firstCall.args[0]).to.eql({
Expand Down Expand Up @@ -753,7 +753,7 @@ limit=${limit}&offset=${offset}`,
};
client.createEvent(request, true)
.catch((error) => {
error.should.be.instanceOf(cchedulerErrorStubbed);
error.should.be.instanceOf(cronicleErrorStubbed);
expect(error.code).to.eql('unique');
expect(error.message).to.eql('event already exists');
expect(requestStub.firstCall.args[0]).to.eql({
Expand Down Expand Up @@ -792,7 +792,7 @@ limit=${limit}&offset=${offset}`,
};
client.createEvent(request)
.catch((error) => {
error.should.be.instanceOf(cchedulerErrorStubbed);
error.should.be.instanceOf(cronicleErrorStubbed);
expect(error.code).to.eql(code);
expect(error.message).to.eql(description);
done();
Expand Down Expand Up @@ -869,7 +869,7 @@ limit=${limit}&offset=${offset}`,
requestStub.resolves({code, description});
client.updateEvent({id})
.catch((error) => {
error.should.be.instanceOf(cchedulerErrorStubbed);
error.should.be.instanceOf(cronicleErrorStubbed);
expect(error.code).to.eql(code);
expect(error.message).to.eql(description);
done();
Expand Down Expand Up @@ -939,7 +939,7 @@ limit=${limit}&offset=${offset}`,
requestStub.resolves({code, description});
client.updateJob({id})
.catch((error) => {
error.should.be.instanceOf(cchedulerErrorStubbed);
error.should.be.instanceOf(cronicleErrorStubbed);
expect(error.code).to.eql(code);
expect(error.message).to.eql(description);
done();
Expand Down Expand Up @@ -1008,7 +1008,7 @@ limit=${limit}&offset=${offset}`,
requestStub.resolves({code, description});
client.deleteEvent({id})
.catch((error) => {
error.should.be.instanceOf(cchedulerErrorStubbed);
error.should.be.instanceOf(cronicleErrorStubbed);
expect(error.code).to.eql(code);
expect(error.message).to.eql(description);
done();
Expand Down Expand Up @@ -1075,7 +1075,7 @@ limit=${limit}&offset=${offset}`,
requestStub.resolves({code, description});
client.abortJob({id})
.catch((error) => {
error.should.be.instanceOf(cchedulerErrorStubbed);
error.should.be.instanceOf(cronicleErrorStubbed);
expect(error.code).to.eql(code);
expect(error.message).to.eql(description);
done();
Expand Down Expand Up @@ -1139,7 +1139,7 @@ limit=${limit}&offset=${offset}`,
requestStub.resolves({code, description});
client.getJobStatus({id})
.catch((error) => {
error.should.be.instanceOf(cchedulerErrorStubbed);
error.should.be.instanceOf(cronicleErrorStubbed);
expect(error.code).to.eql(code);
expect(error.message).to.eql(description);
done();
Expand Down

0 comments on commit c079333

Please sign in to comment.