Broken pipe (Errno::EPIPE)

前端 未结 4 660
迷失自我
迷失自我 2020-12-05 04:05

i have a Broken pipe (Errno::EPIPE) error popping up and i don\'t understand what it is or how to fix it. the full error is:

example.rb:19:in `w         


        
4条回答
  •  清歌不尽
    2020-12-05 04:50

    Although signal traps do work, as tokland said, they are defined application wide and can cause some unexpected behavior if you want to handle a broken pipe in some other way somewhere else in your app.

    I'd suggest just using a standard rescue since the error still inherits from StandardError. More about this module of errors: http://ruby-doc.org/core-2.0.0/Errno.html

    Example:

    begin
      vari.print("x=" + my_val + "&y=1&z=Add+Num\r\n")
    rescue Errno::EPIPE
      puts "Connection broke!"
    end
    

    Edit: It's important to note (as @mklement0 does in the comments) that if you were originally piping your output using puts to something expecting output on STDOUT, the final puts in the code above will raise another Errno::EPIPE exception. It's probably better practice to use STDERR.puts anyway.

    begin
      vari.print("x=" + my_val + "&y=1&z=Add+Num\r\n")
    rescue Errno::EPIPE
      STDERR.puts "Connection broke!"
    end
    

提交回复
热议问题