Skip to content

Commit

Permalink
Merge pull request #11 from SoftwareAG/fix/c8yclient-throwErrByPath
Browse files Browse the repository at this point in the history
Fix use of Cypress.util.throwErrByPath
  • Loading branch information
thomaswinkler authored Oct 30, 2023
2 parents e4f1678 + 3e31e59 commit 8117e99
Showing 1 changed file with 18 additions and 54 deletions.
72 changes: 18 additions & 54 deletions lib/commands/c8yclient.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
restoreClient,
storeClient,
tenantFromBasicAuth,
throwError
throwError,
} from "./utils";
const { _ } = Cypress;

Expand Down Expand Up @@ -302,29 +302,10 @@ function run(client, fns, prevSubject, options, baseUrl) {
throw err;
}

// throwErrByPath does not work as it seems to not correctly wrap $errutils.throwErrByPath
// needs a Cypress issue to clarify. currently not good workaround possible other than
// copying a lot of code from Cypress. needs to be revisited.
if (err.body && err.body.error) {
throwError(makeErrorMessage(err));

// Cypress.utils.throwErrByPath("request.c8yclient_status_invalid", {
// onFail: options._log,
// errorProps: err,
// stack: true,
// });
} else {
// todo: throwErrByPath with request.status_invalid to get expected cy.request error message
throwError(makeErrorMessage(err));

// Cypress.utils.throwErrByPath("request.status_invalid", {
// onFail: options._log,
// args: {
// url,
// },
// stack: true,
// });
}
Cypress.utils.throwErrByPath("request.c8yclient_status_invalid", {
args: err,
stack: false,
});
});

if (isArrayOfFunctions(fns) && !_.isEmpty(fns)) {
Expand Down Expand Up @@ -352,25 +333,19 @@ function makeErrorMessage(obj) {
const err = obj.args || obj.errorProps || obj;
const body = err.body || {};

return cyStripIndent(
`\
c8yclient failed with: ${err.status} (${err.statusText})
${err.url}
The response we received from Cumulocity was:
${_.isObject(body) ? JSON.stringify(body, null, 2) : body.toString()}
For more information check:
${
(err.body && err.body.info) ||
"https://github.com/SoftwareAG/cumulocity-cypress"
}
\n
`,
8
);
const message = [
`c8yclient failed with: ${err.status} (${err.statusText})`,
`${err.url}`,
`The response we received from Cumulocity was: `,
`${_.isObject(body) ? JSON.stringify(body, null, 2) : body.toString()}`,
`For more information check:`,
`${
(err.body && err.body.info) ||
"https://github.com/SoftwareAG/cumulocity-cypress"
}`,
`\n`,
].join(`\n`);
return message;
}

async function responseObject(
Expand Down Expand Up @@ -399,17 +374,6 @@ function getDisplayUrl(url, baseUrl = Cypress.config().baseUrl) {
return url.replace(baseUrl, "");
}

function cyStripIndent(str, indentSize = 10) {
const indent = " ".repeat(indentSize);

return str
.split("\n")
.map((s) => {
return s.startsWith(indent) ? s.substring(indentSize) : s;
})
.join("\n");
}

function isArrayOfFunctions(functions) {
if (!functions || !_.isArray(functions)) return false;
return _.isEmpty(functions.filter((f) => !_.isFunction(f)));
Expand Down

0 comments on commit 8117e99

Please sign in to comment.