Use create-error for better error creation
This commit is contained in:
parent
5ec9c2b029
commit
20dc871303
7 changed files with 46 additions and 38 deletions
|
|
@ -1,6 +1,6 @@
|
|||
import { FileStore } from './filestore';
|
||||
|
||||
var CHANNEL_STORE = new FileStore();
|
||||
const CHANNEL_STORE = new FileStore();
|
||||
|
||||
export function load(channelName) {
|
||||
return CHANNEL_STORE.load(channelName);
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ import * as Promise from 'bluebird';
|
|||
import { stat } from 'fs';
|
||||
import * as fs from 'graceful-fs';
|
||||
import path from 'path';
|
||||
import { ChannelStateSizeError } from '../errors';
|
||||
|
||||
const readFileAsync = Promise.promisify(fs.readFile);
|
||||
const writeFileAsync = Promise.promisify(fs.writeFile);
|
||||
|
|
@ -18,7 +19,10 @@ export class FileStore {
|
|||
const filename = this.filenameForChannel(channelName);
|
||||
return statAsync(filename).then(stats => {
|
||||
if (stats.size > SIZE_LIMIT) {
|
||||
throw new Error('Channel state file is too large: ' + stats.size);
|
||||
throw new ChannelStateSizeError('Channel state file is too large', {
|
||||
limit: SIZE_LIMIT,
|
||||
actual: stats.size
|
||||
});
|
||||
} else {
|
||||
return readFileAsync(filename);
|
||||
}
|
||||
|
|
@ -34,11 +38,12 @@ export class FileStore {
|
|||
save(channelName, data) {
|
||||
const filename = this.filenameForChannel(channelName);
|
||||
const fileContents = new Buffer(JSON.stringify(data), 'utf8');
|
||||
if (fileContents.length > SIZE_LIMIT) {
|
||||
let error = new Error('Channel state size is too large');
|
||||
error.limit = SIZE_LIMIT;
|
||||
error.size = fileContents.length;
|
||||
return Promise.reject(error);
|
||||
if (fileContents.length > 0*SIZE_LIMIT) {
|
||||
return Promise.reject(new ChannelStateSizeError(
|
||||
'Channel state size is too large', {
|
||||
limit: SIZE_LIMIT,
|
||||
actual: fileContents.length
|
||||
}));
|
||||
}
|
||||
|
||||
return writeFileAsync(filename, fileContents);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue