GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — compat: stream:error -> [res|res]:error broadcasting · Issue #168 · nodejs/http2

This repository was archived by the owner on Jul 6, 2018. It is now read-only.
This repository was archived by the owner on Jul 6, 2018. It is now read-only.

compat: stream:error -> [res|res]:error broadcasting #168

Copy link
Copy link
Closed
Closed
compat: stream:error -> [res|res]:error broadcasting#168
Copy link

Description

@sebdeckers
sebdeckers
opened on Jul 6, 2017
Issue body actions

I'm using Http2Stream.respondWithFile from an app that also uses the compatibility layer. Error handling is getting pretty bloated, IMO, because the stream error event is broadcasted to both request and response.

Current code:

const noop = () => {}
request.on('error', noop)
response.on('error', noop)
response.stream.on('error', () => {
  request.removeListener('error', noop)
  response.removeListener('error', noop)
  if (response.stream.destroyed === false) {
    response.writeHead(500, {'content-type': 'text/plain'})
    response.end('Internal Server Error')
  }
})
response.stream.respondWithFile('/my/file/path', response.getHeaders())

Desired code:

response.stream.on('error', () => {
  if (response.stream.destroyed === false) {
    response.writeHead(500, {'content-type': 'text/plain'})
    response.end('Internal Server Error')
  }
})
response.stream.respondWithFile('/my/file/path', response.getHeaders())

What would be needed is to rename or stop propagating stream error events. It's a very simple change in the two error event handlers (for res and req). However I'm hesitant because I don't know what the thought process was behind that broadcasting.

Perhaps related, socket:error events do not seem to bubble up to the req/res instances in the http1 layer.

@jasnell @mcollina Thoughts?

Activity

  1. mcollina commented on Jul 6, 2017

    @mcollina
    mcollina
    on Jul 6, 2017
    SponsorMember
    More actions

    I'm not convinced that:

    response.stream.on('error', () => {
      if (response.stream.destroyed === false) {
        response.writeHead(500, {'content-type': 'text/plain'})
        response.end('Internal Server Error')
      }
    })
    response.stream.respondWithFile('/my/file/path', response.getHeaders())

    Is nice, specifically on the response.stream.on('error' part.

    request.on('error', noop)
    response.on('error', noop)

    is also very bad practice.

    Errors on the underlining socket bubble up differently in require('http'), see https://github.com/nodejs/node/blob/master/lib/_http_server.js#L45. Maybe we should not forward them, and rout them to server.on('clientError').

    Reacted by Sebastiaan Deckers
  2. sebdeckers commented on Jul 6, 2017

    @sebdeckers
    sebdeckers
    on Jul 6, 2017
    ContributorAuthor
    More actions

    @mcollina Apologies for the confusion. I totally agree, the noop handlers are egregious — I'm merely highlighting that handling of an underlying stream error in two distinct event handlers is awkward, yet not having both listeners would throw an 'uncaught error event' warning. I'm also leaning towards the clientError idea, so that handling the lower level event is possible, yet optional. I am happy to roll a small PR (patch + tests) for this if there is support.

  3. mcollina commented on Jul 17, 2017

    @mcollina
    mcollina
    on Jul 17, 2017
    SponsorMember
    More actions

    @jasnell can you please have a look?

  4. jasnell commented on Jul 17, 2017

    @jasnell
    jasnell
    on Jul 17, 2017
    Member
    More actions

    I'm thinking that for the compatibility layer, errors that are happening on the server side should likely only be broadcast to the res object unless those specifically have to do with handling in the request. In other words, any stream.on('error') forwarding would go to res only.

  5. jasnell
    marked this as a duplicate of Usability issue with respondWithFile #183 on Jul 19, 2017
  6. jasnell
    closed this as completedon Oct 24, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.