Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix that non-successful message responses were not displayed in ACE editor #1810

Merged
merged 2 commits into from
Nov 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 32 additions & 7 deletions ui/modules/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,14 @@ export function setAuthHeader(forDevOps) {
}
}

function showDittoError(dittoErr, response) {
if (dittoErr.status && dittoErr.message) {
Utils.showError(dittoErr.description + `\n(${dittoErr.error})`, dittoErr.message, dittoErr.status);
} else {
Utils.showError(JSON.stringify(dittoErr), 'Error', response.status);
}
}

/**
* Calls the Ditto api
* @param {String} method 'POST', 'GET', 'DELETE', etc.
Expand All @@ -315,10 +323,16 @@ export function setAuthHeader(forDevOps) {
* @param {Object} additionalHeaders object with additional header fields
* @param {boolean} returnHeaders request full response instead of json content
* @param {boolean} devOps default: false. Set true to avoid /api/2 path
* @param {boolean} returnErrorJson default: false. Set true to return the response of a failed HTTP call as JSON
* @return {Object} result as json object
*/
export async function callDittoREST(method, path, body = null,
additionalHeaders = null, returnHeaders = false, devOps = false): Promise<any> {
export async function callDittoREST(method,
path,
body = null,
additionalHeaders = null,
returnHeaders = false,
devOps = false,
returnErrorJson = false): Promise<any> {
let response;
const contentType = method === 'PATCH' ? 'application/merge-patch+json' : 'application/json';
try {
Expand All @@ -336,14 +350,25 @@ export async function callDittoREST(method, path, body = null,
throw err;
}
if (!response.ok) {
response.json()
if (returnErrorJson) {
if (returnHeaders) {
return response;
} else {
return response.json().then((dittoErr) => {
showDittoError(dittoErr, response);
return dittoErr;
});
}
} else {
response.json()
.then((dittoErr) => {
Utils.showError(dittoErr.description + `\n(${dittoErr.error})`, dittoErr.message, dittoErr.status);
showDittoError(dittoErr, response);
})
.catch((err) => {
Utils.showError('No error details from Ditto', response.statusText, response.status);
});
throw new Error('An error occurred: ' + response.status);
throw new Error('An error occurred: ' + response.status);
}
}
if (response.status !== 204) {
if (returnHeaders) {
Expand Down Expand Up @@ -409,7 +434,7 @@ export async function callConnectionsAPI(operation, successCallback, connectionI
if (!response.ok) {
response.json()
.then((dittoErr) => {
Utils.showError(dittoErr.description, dittoErr.message, dittoErr.status);
showDittoError(dittoErr, response);
})
.catch((err) => {
Utils.showError('No error details from Ditto', response.statusText, response.status);
Expand All @@ -422,7 +447,7 @@ export async function callConnectionsAPI(operation, successCallback, connectionI
.then((data) => {
if (data && data['?'] && data['?']['?'].status >= 400) {
const dittoErr = data['?']['?'].payload;
Utils.showError(dittoErr.description, dittoErr.message, dittoErr.status);
showDittoError(dittoErr, response);
} else {
if (params.unwrapJsonPath) {
params.unwrapJsonPath.split('.').forEach(function(node) {
Expand Down
14 changes: 9 additions & 5 deletions ui/modules/things/featureMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,14 @@ function messageFeature() {
}
aceResponse.setValue('');
API.callDittoREST('POST', '/things/' + Things.theThing.thingId +
'/features/' + theFeatureId +
'/inbox/messages/' + dom.inputMessageSubject.value +
'?timeout=' + dom.inputMessageTimeout.value,
payload,
'/features/' + theFeatureId +
'/inbox/messages/' + dom.inputMessageSubject.value +
'?timeout=' + dom.inputMessageTimeout.value,
payload,
null,
false,
false,
true
).then((data) => {
dom.buttonMessageSend.classList.remove('busy');
dom.buttonMessageSend.disabled = false;
Expand All @@ -137,7 +141,7 @@ function messageFeature() {
}).catch((err) => {
dom.buttonMessageSend.classList.remove('busy');
dom.buttonMessageSend.disabled = false;
aceResponse.setValue('');
aceResponse.setValue(`Error: ${err}`);
});
}

Expand Down
12 changes: 8 additions & 4 deletions ui/modules/things/thingMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,13 @@ function messageThing() {
}
aceResponse.setValue('');
API.callDittoREST('POST', '/things/' + Things.theThing.thingId +
'/inbox/messages/' + dom.inputThingMessageSubject.value +
'?timeout=' + dom.inputThingMessageTimeout.value,
payload,
'/inbox/messages/' + dom.inputThingMessageSubject.value +
'?timeout=' + dom.inputThingMessageTimeout.value,
payload,
null,
false,
false,
true
).then((data) => {
dom.buttonThingMessageSend.classList.remove('busy');
dom.buttonThingMessageSend.disabled = false;
Expand All @@ -129,7 +133,7 @@ function messageThing() {
}).catch((err) => {
dom.buttonThingMessageSend.classList.remove('busy');
dom.buttonThingMessageSend.disabled = false;
aceResponse.setValue('');
aceResponse.setValue(`Error: ${err}`);
});
}

Expand Down
Loading