From aa6e8e2e28f5fccf4f04e683a8f3b6fcfe1abdcb Mon Sep 17 00:00:00 2001 From: AtofStryker Date: Wed, 1 May 2024 13:45:25 -0400 Subject: [PATCH] fix: handle promises rejected with undefined gracefully --- cli/CHANGELOG.md | 4 +++ .../driver/cypress/e2e/issues/23927.cy.js | 15 +++++++++++ .../cypress/fixtures/throws-undefined.html | 13 ++++++++++ packages/driver/src/cypress/error_utils.ts | 25 +++++++++++-------- yarn.lock | 8 +++--- 5 files changed, 51 insertions(+), 14 deletions(-) create mode 100644 packages/driver/cypress/e2e/issues/23927.cy.js create mode 100644 packages/driver/cypress/fixtures/throws-undefined.html diff --git a/cli/CHANGELOG.md b/cli/CHANGELOG.md index 667293ed0cbc..605c964651b9 100644 --- a/cli/CHANGELOG.md +++ b/cli/CHANGELOG.md @@ -3,6 +3,10 @@ _Released 5/7/2024 (PENDING)_ +**Bugfixes:** + +- Fixed a bug where promises rejected with `undefined` were failing inside `cy.origin()`. Addresses [#23937](/~https://github.com/cypress-io/cypress/issues/23937). + **Dependency Updates:** - Updated electron from `27.1.3` to `27.3.10` to address [CVE-2024-3156](https://nvd.nist.gov/vuln/detail/CVE-2024-3156). Addressed in [#29367](/~https://github.com/cypress-io/cypress/pull/29367). diff --git a/packages/driver/cypress/e2e/issues/23927.cy.js b/packages/driver/cypress/e2e/issues/23927.cy.js new file mode 100644 index 000000000000..20210995e1bf --- /dev/null +++ b/packages/driver/cypress/e2e/issues/23927.cy.js @@ -0,0 +1,15 @@ +// /~https://github.com/cypress-io/cypress/issues/23927 +describe('issue 23927', () => { + it('Fails gracefully if origin page throws undefined', () => { + cy.visit('http://barbaz.com:3500/fixtures/generic.html') + cy.origin('http://foobar.com:3500', () => { + Cypress.on('uncaught:exception', (err, runnable) => { + expect(err.message).to.contain('An unknown error has occurred: undefined') + + return false + }) + + cy.visit('http://foobar.com:3500/fixtures/throws-undefined.html') + }) + }) +}) diff --git a/packages/driver/cypress/fixtures/throws-undefined.html b/packages/driver/cypress/fixtures/throws-undefined.html new file mode 100644 index 000000000000..9b5a29551194 --- /dev/null +++ b/packages/driver/cypress/fixtures/throws-undefined.html @@ -0,0 +1,13 @@ + + + + Promise.reject undefined + + + +

I am going to reject undefined in the document

+ + + \ No newline at end of file diff --git a/packages/driver/src/cypress/error_utils.ts b/packages/driver/src/cypress/error_utils.ts index 9389a2cec8b3..c79e8e694347 100644 --- a/packages/driver/src/cypress/error_utils.ts +++ b/packages/driver/src/cypress/error_utils.ts @@ -188,23 +188,28 @@ const appendErrMsg = (err, errMsg) => { }) } -const makeErrFromObj = (obj) => { +const makeErrFromObj = (obj: any) => { if (_.isString(obj)) { return new Error(obj) } - const err2 = new Error(obj.message) + if (_.isObject(obj)) { + const err2 = new Error((obj as any).message) - err2.name = obj.name - err2.stack = obj.stack + err2.name = (obj as any).name + err2.stack = (obj as any).stack - _.each(obj, (val, prop) => { - if (!err2[prop]) { - err2[prop] = val - } - }) + _.each(obj, (val, prop) => { + if (!err2[prop]) { + err2[prop] = val + } + }) + + return err2 + } - return err2 + // handle the error gracefully in the case a promise is rejected with undefined + return new Error(`An unknown error has occurred: ${obj}`) } const makeErrFromErr = (err, options: any = {}) => { diff --git a/yarn.lock b/yarn.lock index 2356fc0b7ff7..f679e20e25e3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -14334,10 +14334,10 @@ ee-first@1.1.1: resolved "https://registry.yarnpkg.com/ee-first/-/ee-first-1.1.1.tgz#590c61156b0ae2f4f0255732a158b266bc56b21d" integrity sha1-WQxhFWsK4vTwJVcyoViyZrxWsh0= -ejs@^3.1.7, ejs@^3.1.8: - version "3.1.9" - resolved "https://registry.yarnpkg.com/ejs/-/ejs-3.1.9.tgz#03c9e8777fe12686a9effcef22303ca3d8eeb361" - integrity sha512-rC+QVNMJWv+MtPgkt0y+0rVEIdbtxVADApW9JXrUVlzHetgcyczP/E7DJmWJ4fJCZF2cPcBk0laWO9ZHMG3DmQ== +ejs@^3.1.10, ejs@^3.1.7: + version "3.1.10" + resolved "https://registry.yarnpkg.com/ejs/-/ejs-3.1.10.tgz#69ab8358b14e896f80cc39e62087b88500c3ac3b" + integrity sha512-UeJmFfOrAQS8OJWPZ4qtgHyWExa088/MtK5UEyoJGFH67cDEXkZSviOiKRCZ4Xij0zxI3JECgYs3oKx+AizQBA== dependencies: jake "^10.8.5"