Skip to content

Commit c342a58

Browse files
committed
feat: added back no referrer and >= 500 status check (4xx request will redirect back now like older versions)
1 parent f7162ea commit c342a58

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

index.js

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const path = require('path');
33
const process = require('process');
44
const { Buffer } = require('buffer');
55

6+
const co = require('co');
67
const Boom = require('@hapi/boom');
78
const camelCase = require('camelcase');
89
const capitalize = require('capitalize');
@@ -88,6 +89,7 @@ const passportLocalMongooseTooManyRequests = new Set([
8889
//
8990

9091
function errorHandler(
92+
cookiesKey = false,
9193
_logger = console,
9294
useCtxLogger = true, // useful if you have ctx.logger (e.g. you're using Cabin's middleware)
9395
stringify = fastSafeStringify // you could alternatively use JSON.stringify
@@ -176,6 +178,9 @@ function errorHandler(
176178
// check if there is a view rendering engine binding `this.render`
177179
const hasRender = _isFunction(this.render);
178180

181+
// check if we're about to go into a possible endless redirect loop
182+
const noReferrer = this.get('Referrer') === '';
183+
179184
// populate the status and body with `boom` error message payload
180185
// (e.g. you can do `ctx.throw(404)` and it will output a beautiful err obj)
181186
err.status = err.status || 500;
@@ -219,7 +224,7 @@ function errorHandler(
219224
} else {
220225
this.body = _404;
221226
}
222-
} else {
227+
} else if (noReferrer || this.status >= 500) {
223228
// flash an error message
224229
if (hasFlash) this.flash('error', err.message);
225230

@@ -234,6 +239,53 @@ function errorHandler(
234239
} else {
235240
this.body = _500;
236241
}
242+
} else {
243+
//
244+
// attempt to redirect the user back
245+
//
246+
247+
// flash an error message
248+
if (hasFlash) this.flash('error', err.message);
249+
250+
// TODO: until the issue is resolved, we need to add this here
251+
// <https://github.com/koajs/generic-session/pull/95#issuecomment-246308544>
252+
if (
253+
this.sessionStore &&
254+
this.sessionId &&
255+
this.session &&
256+
cookiesKey
257+
) {
258+
try {
259+
await co
260+
.wrap(this.sessionStore.set)
261+
.call(this.sessionStore, this.sessionId, this.session);
262+
this.cookies.set(cookiesKey, this.sessionId, this.session.cookie);
263+
} catch (err) {
264+
logger.error(err);
265+
if (err.code === 'ERR_HTTP_HEADERS_SENT') return;
266+
}
267+
}
268+
269+
/*
270+
// TODO: we need to add support for `koa-session-store` here
271+
// <https://github.com/koajs/generic-session/pull/95#issuecomment-246308544>
272+
//
273+
// these comments may no longer be valid and need reconsidered:
274+
//
275+
// if we're using `koa-session-store` we need to add
276+
// `this._session = new Session()`, and then run this:
277+
await co.wrap(this._session._store.save).call(
278+
this._session._store,
279+
this._session._sid,
280+
stringify(this.session)
281+
);
282+
this.cookies.set(this._session._name, stringify({
283+
_sid: this._session._sid
284+
}), this._session._cookieOpts);
285+
*/
286+
287+
// redirect the user to the page they were just on
288+
this.redirect('back');
237289
}
238290

239291
break;

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"@hapi/boom": "^10.0.0",
3535
"camelcase": "6",
3636
"capitalize": "^2.0.4",
37+
"co": "^4.6.0",
3738
"fast-safe-stringify": "^2.1.1",
3839
"html-to-text": "^8.2.0",
3940
"humanize-string": "2",

0 commit comments

Comments
 (0)