Skip to content

Commit 0c823e9

Browse files
committed
Improve error logging
1 parent 9700fb1 commit 0c823e9

File tree

2 files changed

+52
-30
lines changed

2 files changed

+52
-30
lines changed

packages/proxy/lib/http/index.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { DeferredSourceMapCache } from '@packages/rewriter'
2222
import type { RemoteStates } from '@packages/server/lib/remote_states'
2323
import type { CookieJar } from '@packages/server/lib/cookie-jar'
2424
import type { Automation } from '@packages/server/lib/automation/automation'
25+
import { errorUtils } from '@packages/errors'
2526

2627
function getRandomColorFn () {
2728
return chalk.hex(`#${Number(
@@ -156,11 +157,13 @@ export function _runStage (type: HttpStages, ctx: any, onError) {
156157
const fullCtx = {
157158
next: () => {
158159
fullCtx.next = () => {
159-
if (type === HttpStages.Error) {
160-
ctx.debug('`this.next` called more than once within the same middleware function but was ignored due to being in an Error flow. %o', { middlewareName, error: ctx.error })
161-
} else {
162-
throw new Error(`Error running proxy middleware: Cannot call this.next() more than once in the same middleware function. Doing so can cause unintended issues.`)
160+
const error = new Error('Error running proxy middleware: Detected `this.next()` was called more than once in the same middleware function. This can cause unintended issues and may indicate an error in your middleware logic or configuration.')
161+
162+
if (ctx.error) {
163+
error.message = error.message += `\nThis middleware invocation previously encountered an error which may be related: ${ctx.error}`
163164
}
165+
166+
throw error
164167
}
165168

166169
copyChangedCtx()
@@ -194,10 +197,7 @@ export function _runStage (type: HttpStages, ctx: any, onError) {
194197
try {
195198
middleware.call(fullCtx)
196199
} catch (err) {
197-
// TODO Find better way to log this
198-
// eslint-disable-next-line no-console
199-
console.error(err)
200-
200+
errorUtils.logError(err)
201201
// This is a total failure so we will not be retrying the request, clear it before executing handler
202202
// If we do not do this we will end up attempting to double-execute the middleware `next` method
203203
// https://github.com/cypress-io/cypress/issues/22825

packages/proxy/test/unit/http/response-middleware.spec.ts

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,33 +31,55 @@ describe('http/response-middleware', function () {
3131
])
3232
})
3333

34-
it('errors if this.next() is called more than once in the same middleware', function (done) {
35-
const middleware = function () {
36-
this.next()
37-
this.next()
38-
}
34+
describe('multiple this.next invocations', () => {
35+
context('within the same middleware', () => {
36+
it('throws an error', function (done) {
37+
const middleware = function () {
38+
this.next()
39+
this.next()
40+
}
41+
42+
testMiddleware([middleware], {
43+
onError (err) {
44+
expect(err.message).to.equal('Error running proxy middleware: Detected `this.next()` was called more than once in the same middleware function. This can cause unintended issues and may indicate an error in your middleware logic or configuration.')
45+
46+
done()
47+
},
48+
})
49+
})
3950

40-
testMiddleware([middleware], {
41-
onError (err) {
42-
expect(err.message).to.equal('Error running proxy middleware: Cannot call this.next() more than once in the same middleware function. Doing so can cause unintended issues.')
51+
it('includes a previous context error in error message if one exists', (done) => {
52+
const middleware = function () {
53+
this.next()
54+
this.next()
55+
}
4356

44-
done()
45-
},
57+
testMiddleware([middleware], {
58+
error: new Error('previous error'),
59+
onError (err) {
60+
expect(err.message).to.contain('This middleware invocation previously encountered an error which may be related: Error: previous error')
61+
62+
done()
63+
},
64+
})
65+
})
4666
})
47-
})
4867

49-
it('does not error if this.next() is called more than once in different middleware', function () {
50-
const middleware1 = function () {
51-
this.next()
52-
}
53-
const middleware2 = function () {
54-
this.next()
55-
}
68+
context('across different middleware', () => {
69+
it('does not throw an error', function () {
70+
const middleware1 = function () {
71+
this.next()
72+
}
73+
const middleware2 = function () {
74+
this.next()
75+
}
5676

57-
return testMiddleware([middleware1, middleware2], {
58-
onError () {
59-
throw new Error('onError should not be called')
60-
},
77+
return testMiddleware([middleware1, middleware2], {
78+
onError () {
79+
throw new Error('onError should not be called')
80+
},
81+
})
82+
})
6183
})
6284
})
6385

0 commit comments

Comments
 (0)