lib/middleware/website.js
async function onerror(err) {
  // don't do anything if there is no error.
  // this allows you to pass `this.onerror`
  // to node-style callbacks.
  if (null == err) return;

  if (!(err instanceof Error))
    err = new Error(format("non-error thrown: %j", err));

  let headerSent = false;
  if (this.headerSent || !this.writable) {
    headerSent = err.headerSent = true;
  }

  // delegate
  this.app.emit("error", err, this);

  // nothing we can do here other
  // than delegate to the app-level
  // handler and log.
  if (headerSent) {
    return;
  }

  const { res } = this;

  if (!(err instanceof S3Error)) {
    err = S3Error.fromError(err);
  }

  // first unset all headers
  res
    .getHeaderNames()
    .filter(name => !name.match(/^access-control-|vary|x-amz-/i))
    .forEach(name => res.removeHeader(name));

  // (the presence of x-amz-error-* headers needs additional research)
  // this.set(err.headers);

  // force text/html
  this.type = "text/html";

  if (!err.description)
    err.description = `${err.status} ${statuses[err.status]}`;

  // respond
  const { website } = this.state;
  if (
    err.code !== "NoSuchBucket" &&
    err.code !== "UnsupportedQuery" &&
    website.errorDocumentKey
  ) {
    // attempt to serve error document
    const object = await this.store.getObject(
      this.params.bucket,
      website.errorDocumentKey
    );
    if (object) {
      const objectRedirectLocation =
        object.metadata["x-amz-website-redirect-location"];
      if (objectRedirectLocation) {
        object.content.destroy();
        this.status = 301;
        this.redirect(objectRedirectLocation);
        res.end(this.body);
      } else {
        this.type = object.metadata["content-type"];
        this.length = object.size;
        object.content.pipe(res);
      }
      return;
    }
    this.logger.error(
      "Custom Error Document not found: " + website.errorDocumentKey
    );
    const errorDocumentErr = new S3Error(
      "NoSuchKey",
      "The specified key does not exist.",
      { Key: website.errorDocumentKey }
    );
    errorDocumentErr.description =
      "An Error Occurred While Attempting to Retrieve a Custom Error Document";
    err.errors.push(errorDocumentErr);
  }

  const msg = err.toHTML();
  this.status = err.status;
  this.length = Buffer.byteLength(msg);
  res.end(msg);
}
async function onerror(err) {
  // don't do anything if there is no error.
  // this allows you to pass `this.onerror`
  // to node-style callbacks.
  if (null == err) return;

  if (!(err instanceof Error))
    err = new Error(format("non-error thrown: %j", err));

  let headerSent = false;
  if (this.headerSent || !this.writable) {
    headerSent = err.headerSent = true;
  }

  // delegate
  this.app.emit("error", err, this);

  // nothing we can do here other
  // than delegate to the app-level
  // handler and log.
  if (headerSent) {
    return;
  }

  const { res } = this;

  if (!(err instanceof S3Error)) {
    err = S3Error.fromError(err);
  }

  // first unset all headers
  res
    .getHeaderNames()
    .filter(name => !name.match(/^access-control-|vary|x-amz-/i))
    .forEach(name => res.removeHeader(name));

  // (the presence of x-amz-error-* headers needs additional research)
  // this.set(err.headers);

  // force text/html
  this.type = "text/html";

  if (!err.description)
    err.description = `${err.status} ${statuses[err.status]}`;

  // respond
  const { website } = this.state;
  if (
    err.code !== "NoSuchBucket" &&
    err.code !== "UnsupportedQuery" &&
    website.errorDocumentKey
  ) {
    // attempt to serve error document
    const object = await this.store.getObject(
      this.params.bucket,
      website.errorDocumentKey
    );
    if (object) {
      const objectRedirectLocation =
        object.metadata["x-amz-website-redirect-location"];
      if (objectRedirectLocation) {
        object.content.destroy();
        this.status = 301;
        this.redirect(objectRedirectLocation);
        res.end(this.body);
      } else {
        this.type = object.metadata["content-type"];
        this.length = object.size;
        object.content.pipe(res);
      }
      return;
    }
    this.logger.error(
      "Custom Error Document not found: " + website.errorDocumentKey
    );
    const errorDocumentErr = new S3Error(
      "NoSuchKey",
      "The specified key does not exist.",
      { Key: website.errorDocumentKey }
    );
    errorDocumentErr.description =
      "An Error Occurred While Attempting to Retrieve a Custom Error Document";
    err.errors.push(errorDocumentErr);
  }

  const msg = err.toHTML();
  this.status = err.status;
  this.length = Buffer.byteLength(msg);
  res.end(msg);
}
async function onerror ( err ) { // don't do anything if there is no error. // this allows you to pass `this.onerror` // to node-style callbacks. if ( null == err ) return ; if ( ! ( err instanceof Error ) ) err = new Error ( format ( "non-error thrown: %j" , err ) ) ; let headerSent = false ; if ( this . headerSent || ! this . writable ) { headerSent = err . headerSent = true ; } // delegate this . app . emit ( "error" , err , this ) ; // nothing we can do here other // than delegate to the app-level // handler and log. if ( headerSent ) { return ; } const { res } = this ; if ( ! ( err instanceof S3Error ) ) { err = S3Error . fromError ( err ) ; } // first unset all headers res . getHeaderNames ( ) . filter ( name => ! name . match ( / ^access-control-|vary|x-amz- / i ) ) . forEach ( name => res . removeHeader ( name ) ) ; // (the presence of x-amz-error-* headers needs additional research) // this.set(err.headers); // force text/html this . type = "text/html" ; if ( ! err . description ) err . description = ` ${ err . status } ${ statuses [ err . status ] } ` ; // respond const { website } = this . state ; if ( err . code !== "NoSuchBucket" && err . code !== "UnsupportedQuery" && website . errorDocumentKey ) { // attempt to serve error document const object = await this . store . getObject ( this . params . bucket , website . errorDocumentKey ) ; if ( object ) { const objectRedirectLocation = object . metadata [ "x-amz-website-redirect-location" ] ; if ( objectRedirectLocation ) { object . content . destroy ( ) ; this . status = 301 ; this . redirect ( objectRedirectLocation ) ; res . end ( this . body ) ; } else { this . type = object . metadata [ "content-type" ] ; this . length = object . size ; object . content . pipe ( res ) ; } return ; } this . logger . error ( "Custom Error Document not found: " + website . errorDocumentKey ) ; const errorDocumentErr = new S3Error ( "NoSuchKey" , "The specified key does not exist." , { Key : website . errorDocumentKey } ) ; errorDocumentErr . description = "An Error Occurred While Attempting to Retrieve a Custom Error Document" ; err . errors . push ( errorDocumentErr ) ; } const msg = err . toHTML ( ) ; this . status = err . status ; this . length = Buffer . byteLength ( msg ) ; res . end ( msg ) ; }
Koa context.onerror handler modified to write a HTML-formatted response body
@param {Error} err
Koa context . onerror handler modified to write a HTML - formatted response body