Skip to content

Commit a8573b0

Browse files
committed
handle transformation error
1 parent 01367ea commit a8573b0

File tree

6 files changed

+101
-2
lines changed

6 files changed

+101
-2
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sir-transformalot",
3-
"version": "1.2.2",
3+
"version": "1.3.0",
44
"description": "Transform data between versions",
55
"main": "sir-transformalot.js",
66
"scripts": {

sir-transformalot.js

+3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ module.exports = function(transforms) {
3535
return callback(err);
3636
}
3737
var transformedData = _transformReadyData(data, fromVersion, toVersion, preparedData, options);
38+
if (transformedData instanceof Error) {
39+
return callback(transformedData);
40+
}
3841
return callback(null, transformedData);
3942
});
4043
}

test/integration/integration.spec.js

+50
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,54 @@ describe('integration tests', function () {
139139
});
140140
});
141141
});
142+
143+
describe('when transform returns an error', () => {
144+
describe('when input data is bad', () => {
145+
before(function (done) {
146+
post('entityReturningErrorOnTransform/v1', {
147+
badInputData: true,
148+
badOutputData: false,
149+
id: 1
150+
}, 500, done);
151+
});
152+
153+
it('should handle them', function () {
154+
expect(bodyReturned).to.containSubset({
155+
message: 'bad data V1toV2'
156+
});
157+
});
158+
});
159+
160+
describe('when output data is bad', () => {
161+
before(function (done) {
162+
post('entityReturningErrorOnTransform/v1', {
163+
badInputData: false,
164+
badOutputData: true,
165+
id: 1
166+
}, 500, done);
167+
});
168+
169+
it('should handle them', function () {
170+
expect(bodyReturned).to.containSubset({
171+
message: 'bad data V2toV1'
172+
});
173+
});
174+
});
175+
176+
describe('when data is good', () => {
177+
before(function (done) {
178+
post('entityReturningErrorOnTransform/v1', {
179+
badInputData: false,
180+
badOutputData: false,
181+
id: 1
182+
}, 200, done);
183+
});
184+
185+
it('should handle them', function () {
186+
expect(bodyReturned).to.containSubset({
187+
id: 1
188+
});
189+
});
190+
});
191+
});
142192
});

test/testApp/app.js

+22
Original file line numberDiff line numberDiff line change
@@ -86,5 +86,27 @@ app.post('/entityNeedingOptions/:version(v2|v1)', function(req, res, next) {
8686
});
8787
});
8888

89+
app.post('/entityReturningErrorOnTransform/:version(v2|v1)', function(req, res, next) {
90+
var parsedId = req.body.id;
91+
var dataVX = req.body;
92+
//upgradeData
93+
transforms.entityReturningErrorOnTransform.transformObject(parsedId, dataVX, req.params.version, 'v2', 'mongo :p', function(err, endVersionData) {
94+
if(err) {
95+
return next(err);
96+
}
97+
//downgrade data
98+
transforms.entityReturningErrorOnTransform.transformObject(parsedId, dataVX, 'v2', req.params.version, 'mongo :p', function(err, endVersionData) {
99+
if(err) {
100+
return next(err);
101+
}
102+
return res.json(endVersionData);
103+
});
104+
});
105+
});
106+
107+
app.use(function (err, req, res, next) {
108+
res.status(500).json({message: err.message});
109+
});
110+
89111
server.listen(8983);
90112
console.log('Starting test app on http://localhost:8983');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var entityReturningErrorOnTransform = {
2+
v2: {
3+
V1toV2: {
4+
transform: function(data, preparedData, options) {
5+
if (data.badInputData) {
6+
return new Error('bad data V1toV2');
7+
}
8+
return data;
9+
}
10+
},
11+
V2toV1: {
12+
transform: function(data, preparedData, options) {
13+
if (data.badOutputData) {
14+
return new Error('bad data V2toV1');
15+
}
16+
return data;
17+
}
18+
}
19+
}
20+
};
21+
22+
var transformalot = require('../../../sir-transformalot');
23+
module.exports = transformalot(entityReturningErrorOnTransform);

test/testApp/transforms/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
module.exports = {
22
entity: require('./entity'),
3-
entityNeedingOptions: require('./entityNeedingOptions')
3+
entityNeedingOptions: require('./entityNeedingOptions'),
4+
entityReturningErrorOnTransform: require('./entityReturningErrorOnTransform')
45
};

0 commit comments

Comments
 (0)