Skip to content

Commit

Permalink
fix: handle promises rejected with undefined gracefully (#29454)
Browse files Browse the repository at this point in the history
* fix: handle promises rejected with undefined gracefully

* improve documentation and type handling
  • Loading branch information
AtofStryker committed May 6, 2024
1 parent 707c841 commit 1396e96
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 14 deletions.
1 change: 1 addition & 0 deletions cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ _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).
- We now pass the same default Chromium flags to Electron as we do to Chrome. As a result of this change, the application under test's `navigator.webdriver` property will now correctly be `true` when testing in Electron. Fixes [#27939](https://github.com/cypress-io/cypress/issues/27939).

**Misc:**
Expand Down
15 changes: 15 additions & 0 deletions packages/driver/cypress/e2e/issues/23927.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// https://github.com/cypress-io/cypress/issues/23927
describe('issue 23927', { browser: '!webkit' }, () => {
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')
})
})
})
13 changes: 13 additions & 0 deletions packages/driver/cypress/fixtures/throws-undefined.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html>
<head>
<title>Promise.reject undefined</title>
</head>

<body>
<h1> I am going to reject undefined in the document </h1>
</body>
<script>
Promise.reject(undefined)
</script>
</html>
31 changes: 21 additions & 10 deletions packages/driver/src/cypress/error_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,23 +188,34 @@ 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) && _.isString((obj as any).message) && _.isString((obj as any).name)) {
obj = obj as {
message: string
name: string
stack?: string
}

err2.name = obj.name
err2.stack = obj.stack
const err2 = new Error(obj.message)

_.each(obj, (val, prop) => {
if (!err2[prop]) {
err2[prop] = val
}
})
err2.name = (obj as any).name
err2.stack = (obj as any).stack

_.each(obj, (val, prop) => {
if (!err2[prop]) {
err2[prop] = val
}
})

return err2
}

return err2
// handle all other errors gracefully (e.g. a promise is rejected with undefined)
return new Error(`An unknown error has occurred: ${obj}`)
}

const makeErrFromErr = (err, options: any = {}) => {
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down

4 comments on commit 1396e96

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 1396e96 May 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the linux x64 version of the Test Runner.

Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/13.9.0/linux-x64/develop-1396e969daac8c4e8e2fc52e9114f1618e070593/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 1396e96 May 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the linux arm64 version of the Test Runner.

Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/13.9.0/linux-arm64/develop-1396e969daac8c4e8e2fc52e9114f1618e070593/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 1396e96 May 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the darwin x64 version of the Test Runner.

Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/13.9.0/darwin-x64/develop-1396e969daac8c4e8e2fc52e9114f1618e070593/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 1396e96 May 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the win32 x64 version of the Test Runner.

Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/13.9.0/win32-x64/develop-1396e969daac8c4e8e2fc52e9114f1618e070593/cypress.tgz

Please sign in to comment.