Skip to content
Snippets Groups Projects
Verified Commit dba378ea authored by Volker Schukai's avatar Volker Schukai :alien:
Browse files

fix: error in the status processing

parent 5e4df080
No related branches found
No related tags found
No related merge requests found
......@@ -114,44 +114,17 @@ class RestAPI extends Server {
* @throws {Error} the data cannot be read
*/
read() {
const self = this;
let response;
let init = self.getOption('read.init');
if (!isObject(init)) init = {};
if (!init['method']) init['method'] = 'GET';
return new Promise((resolve, reject) => {
fetch(self.getOption('read.url'), init).then(resp => {
response = resp;
const acceptedStatus = self.getOption('read.acceptedStatus', [200]);
if (acceptedStatus.indexOf(resp.status) === -1) {
throw Error('the data cannot be read (response ' + resp.status + ')')
}
return resp.text()
}).then(body => {
let obj;
try {
obj = JSON.parse(body);
} catch (e) {
if (body.length > 100) {
body = body.substring(0, 97) + '...';
}
throw new Error('the response does not contain a valid json (actual: ' + body + ').');
}
return fetchData.call(this, 'read', (obj) => {
self.set(self.transformServerPayload.call(self, obj));
resolve(response);
}).catch(reject);
});
})
}
/**
......@@ -159,6 +132,7 @@ class RestAPI extends Server {
* @throws {WriteError} the data cannot be written
*/
write() {
const self = this;
let init = self.getOption('write.init');
......@@ -168,60 +142,65 @@ class RestAPI extends Server {
'Content-Type': 'application/json'
}
}
if (!init['method']) init['method'] = 'POST';
let obj = self.prepareServerPayload(self.get());
init['body'] = JSON.stringify(obj);
return new Promise((resolve, reject) => {
fetch(self.getOption('write.url'), init).then(response => {
const acceptedStatus = self.getOption('write.acceptedStatus', [200, 201]);
if (acceptedStatus.indexOf(response.status) > -1) {
reject(response);
return;
return fetchData.call(this, init, 'write');
}
response.text().then((body) => {
let obj = {}, validation = {};
try {
obj = JSON.parse(body);
/**
* @return {RestAPI}
*/
getClone() {
const self = this;
return new RestAPI(self[internalSymbol].getRealSubject()['options'].read, self[internalSymbol].getRealSubject()['options'].write);
}
if (reportPath) {
validation = (new Pathfinder(obj)).getVia(reportPath);
}
} catch (e) {
function fetchData(init, key, callback) {
if (body.length > 100) {
body = body.substring(0, 97) + '...';
}
const self = this;
let response;
reject(new Error('the response does not contain a valid json (actual: ' + body + ').'));
return;
}
reject(new WriteError('the data cannot be written (response ' + response.status + ')', response, validation))
return;
return fetch(self.getOption(key + '.url'), init).then(resp => {
response = resp;
const acceptedStatus = self.getOption(key + '.acceptedStatus', [200]);
if (acceptedStatus.indexOf(resp.status) === -1) {
throw Error('the data cannot be ' + key + ' (response ' + resp.status + ')')
}
}).catch(reject);
return resp.text()
}).then(body => {
let obj;
}).catch(reject);
try {
obj = JSON.parse(body);
})
} catch (e) {
if (body.length > 100) {
body = body.substring(0, 97) + '...';
}
throw new Error('the response does not contain a valid json (actual: ' + body + ').');
}
/**
* @return {RestAPI}
*/
getClone() {
const self = this;
return new RestAPI(self[internalSymbol].getRealSubject()['options'].read, self[internalSymbol].getRealSubject()['options'].write);
if (callback && isFunction(callback)) {
callback(obj);
}
return response;
});
}
......
......@@ -28,9 +28,6 @@ describe('RestAPI', function () {
a: "test"
}));
});
},
status: returnStatus
});
......@@ -55,19 +52,23 @@ describe('RestAPI', function () {
});
it('write should ', function (done) {
const ds = new RestAPI({url: 'https://monsterjs.org/assets/world.json'},
{
const ds = new RestAPI({
read: {
url: 'https://monsterjs.org/assets/world.json'
},
write: {
url: 'https://monsterjs.org/assets/world.json',
acceptedStatus: [99]
})
}
}
)
ds.write().then(data => {
done("should not be here");
}).catch(e => done());
});
});
})
describe('rw with errors', function () {
it('read should throw exception', function (done) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment