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

Handle uncaught errors thrown as strings #7637

Merged
merged 3 commits into from
Jun 10, 2020
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
20 changes: 14 additions & 6 deletions packages/driver/src/cy/errors.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const _ = require('lodash')

const $dom = require('../dom')
const $errUtils = require('../cypress/error_utils')

Expand Down Expand Up @@ -33,13 +35,12 @@ const create = (state, config, log) => {
}

const createUncaughtException = (type, args) => {
let [msg, source, lineno, colno, err] = args // eslint-disable-line no-unused-vars
let message
let [message, source, lineno, colno, err] = args // eslint-disable-line no-unused-vars
let docsUrl

// reset the msg on a cross origin script error
// reset the message on a cross origin script error
// since no details are accessible
if (crossOriginScriptRe.test(msg)) {
if (crossOriginScriptRe.test(message)) {
const crossOriginErr = $errUtils.errByPath('uncaught.cross_origin_script')

message = crossOriginErr.message
Expand All @@ -48,8 +49,15 @@ const create = (state, config, log) => {

// if we have the 5th argument it means we're in a modern browser with an
// error object already provided. otherwise, we create one
err = err ?? $errUtils.errByPath('uncaught.error', {
message, source, lineno,
// it's possible the error was thrown as a string (throw 'some error')
// so create it in the case it's not already an object
err = _.isObject(err) ? err : $errUtils.errByPath('uncaught.error', {
source,
lineno,
// if the error was thrown as a string (throw 'some error'), `err` is
// the message ('some error') and message is some browser-created
// variant (e.g. 'Uncaught some error')
message: _.isString(err) ? err : message,
})

err.docsUrl = docsUrl
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ describe('uncaught errors', () => {
done()
})

return cy
cy
.visit('/fixtures/jquery.html')
.window().then((win) => {
return win.$('button:first').on('click', () => {
Expand Down Expand Up @@ -122,7 +122,7 @@ describe('uncaught errors', () => {
done()
})

return cy
cy
.visit('/fixtures/jquery.html')
.window().then((win) => {
return win.$('<a href=\'/fixtures/visit_error.html\'>visit</a>')
Expand All @@ -143,4 +143,24 @@ describe('uncaught errors', () => {

cy.visit('/fixtures/global-error.html')
})

kuceb marked this conversation as resolved.
Show resolved Hide resolved
// https://github.com/cypress-io/cypress/issues/7590
it('creates error object from error that is just a string', (done) => {
cy.once('uncaught:exception', (err) => {
expect(err).not.to.be.a('string')
expect(err.message).to.include('string error')

done()

return false
})

cy
.visit('/fixtures/jquery.html')
.window().then((win) => {
return win.$('button:first').on('click', () => {
throw 'string error'
})
}).get('button:first').click()
})
})