-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f13694c
commit 8a45622
Showing
12 changed files
with
636 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import { name, version } from '../package.json' | ||
|
||
export const USER_AGENT = `${name}/${version}` | ||
|
||
export const JSON_CONTENT_TYPE = 'application/json' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/** | ||
* Copyright (C) 2019, Scale Leap | ||
*/ | ||
|
||
import { ExtendableError } from 'ts-error' | ||
|
||
export interface ErrorObject { | ||
code: string | ||
details: string | ||
requestId: string | ||
} | ||
|
||
export class NullError extends ExtendableError { | ||
public constructor(resource: string) { | ||
super(`Response result is null for "${resource}".`) | ||
} | ||
} | ||
|
||
export class InvalidProgramStateError extends ExtendableError { | ||
public constructor(additionalDetails?: string) { | ||
super( | ||
[ | ||
'This program state should never happen.', | ||
'If you encountered this error, please report it asap.', | ||
additionalDetails, | ||
].join(' '), | ||
) | ||
} | ||
} | ||
|
||
export class SnapshotDownloadError extends ExtendableError { | ||
public constructor(snapshotId: string, snapshotStatus: string) { | ||
super( | ||
[ | ||
'Snapshot must have status equal to SUCCESS before downloading.', | ||
`Got snapshot ${snapshotId} with status ${snapshotStatus} instead.`, | ||
].join(' '), | ||
) | ||
} | ||
} | ||
|
||
export class GenericError extends ExtendableError { | ||
public code: string | ||
|
||
public requestId: string | ||
|
||
public constructor(err: ErrorObject) { | ||
super(err.details) | ||
this.code = err.code | ||
this.requestId = err.requestId | ||
} | ||
} | ||
|
||
export class UnauthorizedError extends GenericError {} | ||
|
||
export class BadRequestError extends GenericError {} | ||
|
||
export class UnprocessableEntityError extends GenericError {} | ||
|
||
export class ResourceNotFoundError extends GenericError {} | ||
|
||
export class NotAcceptableError extends GenericError {} | ||
|
||
export function apiErrorFactory(err: ErrorObject): GenericError { | ||
switch (err.code) { | ||
case 'UNAUTHORIZED': | ||
return new UnauthorizedError(err) | ||
case 'NOT_FOUND': | ||
return new ResourceNotFoundError(err) | ||
case '400': | ||
return new BadRequestError(err) | ||
case '406': | ||
return new NotAcceptableError(err) | ||
case '422': | ||
return new UnprocessableEntityError(err) | ||
default: | ||
return new GenericError(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { gunzip } from 'zlib' | ||
|
||
export default (buffer: Buffer): Promise<Buffer> => { | ||
return new Promise((resolve, reject) => { | ||
return gunzip(buffer, (err, uncompressed) => { | ||
if (err) { | ||
return reject(err) | ||
} | ||
return resolve(uncompressed) | ||
}) | ||
}) | ||
} |
Oops, something went wrong.