all lists on lists.proxmox.com
 help / color / mirror / Atom feed
From: Dominik Csapak <d.csapak@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH proxmox 2/4] proxmox/tools/websocket: improve error handling
Date: Fri, 17 Jul 2020 08:34:49 +0200	[thread overview]
Message-ID: <20200717063451.19207-2-d.csapak@proxmox.com> (raw)
In-Reply-To: <20200717063451.19207-1-d.csapak@proxmox.com>

use io_err_other instead of io_format_err and change the Error type
of create_frame from io::Error to WebSocketError

it is not good to redefine the ErrorKinds of io::Error, since
the caller probably is not aware of that

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
 proxmox/src/tools/websocket.rs | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)

diff --git a/proxmox/src/tools/websocket.rs b/proxmox/src/tools/websocket.rs
index c1b0066..b548b47 100644
--- a/proxmox/src/tools/websocket.rs
+++ b/proxmox/src/tools/websocket.rs
@@ -166,7 +166,7 @@ fn mask_bytes(mask: Option<[u8; 4]>, data: &mut Box<[u8]>) {
 /// ```
 /// # use proxmox::tools::websocket::*;
 /// # use std::io;
-/// # fn main() -> io::Result<()> {
+/// # fn main() -> Result<(), WebSocketError> {
 /// let data = vec![0,1,2,3,4];
 /// let frame = create_frame(None, &data, OpCode::Text)?;
 /// assert_eq!(frame, vec![0b10000001, 5, 0, 1, 2, 3, 4]);
@@ -179,7 +179,7 @@ fn mask_bytes(mask: Option<[u8; 4]>, data: &mut Box<[u8]>) {
 /// ```
 /// # use proxmox::tools::websocket::*;
 /// # use std::io;
-/// # fn main() -> io::Result<()> {
+/// # fn main() -> Result<(), WebSocketError> {
 /// let data = vec![0,1,2,3,4];
 /// let frame = create_frame(Some([0u8, 1u8, 2u8, 3u8]), &data, OpCode::Text)?;
 /// assert_eq!(frame, vec![0b10000001, 0b10000101, 0, 1, 2, 3, 0, 0, 0, 0, 4]);
@@ -192,7 +192,7 @@ fn mask_bytes(mask: Option<[u8; 4]>, data: &mut Box<[u8]>) {
 /// ```
 /// # use proxmox::tools::websocket::*;
 /// # use std::io;
-/// # fn main() -> io::Result<()> {
+/// # fn main() -> Result<(), WebSocketError> {
 /// let data = vec![0,1,2,3,4];
 /// let frame = create_frame(None, &data, OpCode::Ping)?;
 /// assert_eq!(frame, vec![0b10001001, 0b00000101, 0, 1, 2, 3, 4]);
@@ -204,13 +204,13 @@ pub fn create_frame(
     mask: Option<[u8; 4]>,
     data: &[u8],
     frametype: OpCode,
-) -> io::Result<Vec<u8>> {
+) -> Result<Vec<u8>, WebSocketError> {
     let first_byte = 0b10000000 | (frametype as u8);
     let len = data.len();
     if (frametype as u8) & 0b00001000 > 0 && len > 125 {
-        return Err(io::Error::new(
-            ErrorKind::InvalidData,
-            "Control frames cannot have data longer than 125 bytes",
+        return Err(WebSocketError::new(
+                WebSocketErrorKind::Unexpected,
+                "Control frames cannot have data longer than 125 bytes",
         ));
     }
 
@@ -276,7 +276,7 @@ impl<W: AsyncWrite + Unpin> WebSocketWriter<W> {
     }
 
     pub async fn send_control_frame(&mut self, mask: Option<[u8; 4]>, opcode: OpCode, data: &[u8]) -> Result<(), Error> {
-        let frame = create_frame(mask, data, opcode)?;
+        let frame = create_frame(mask, data, opcode).map_err(Error::from)?;
         self.writer.write_all(&frame).await.map_err(Error::from)
     }
 }
@@ -299,7 +299,7 @@ impl<W: AsyncWrite + Unpin> AsyncWrite for WebSocketWriter<W> {
             let frame = match create_frame(this.mask, buf, frametype) {
                 Ok(f) => f,
                 Err(e) => {
-                    return Poll::Ready(Err(e));
+                    return Poll::Ready(Err(io_err_other(e)));
                 }
             };
             this.frame = Some((frame, 0, buf.len()));
@@ -544,12 +544,12 @@ impl<R: AsyncReadExt + Unpin + Send + 'static> AsyncRead for WebSocketReader<R>
                 ReaderState::NoData => {
                     let mut reader = match this.reader.take() {
                         Some(reader) => reader,
-                        None => return Poll::Ready(Err(io_format_err!("no reader"))),
+                        None => return Poll::Ready(Err(io_err_other("no reader"))),
                     };
 
                     let mut buffer = match this.read_buffer.take() {
                         Some(buffer) => buffer,
-                        None => return Poll::Ready(Err(io_format_err!("no buffer"))),
+                        None => return Poll::Ready(Err(io_err_other("no buffer"))),
                     };
 
                     let future = async move {
@@ -576,7 +576,7 @@ impl<R: AsyncReadExt + Unpin + Send + 'static> AsyncRead for WebSocketReader<R>
                 ReaderState::HaveData => {
                     let mut read_buffer = match this.read_buffer.take() {
                         Some(read_buffer) => read_buffer,
-                        None => return Poll::Ready(Err(io_format_err!("no buffer"))),
+                        None => return Poll::Ready(Err(io_err_other("no buffer"))),
                     };
 
                     let mut header = match this.header.take() {
-- 
2.20.1





  reply	other threads:[~2020-07-17  6:34 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-07-17  6:34 [pbs-devel] [PATCH proxmox 1/4] proxmox/tools/websocket: introduce WebSocketError and use it Dominik Csapak
2020-07-17  6:34 ` Dominik Csapak [this message]
2020-07-17  6:34 ` [pbs-devel] [PATCH proxmox 3/4] proxmox/tools/websocket: fix some clippy warnings Dominik Csapak
2020-07-17  6:34 ` [pbs-devel] [PATCH proxmox 4/4] proxmox/tools/byte_buffer: improve read_from example Dominik Csapak
2020-07-17 10:12 ` [pbs-devel] [PATCH proxmox 1/4] proxmox/tools/websocket: introduce WebSocketError and use it Wolfgang Bumiller

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200717063451.19207-2-d.csapak@proxmox.com \
    --to=d.csapak@proxmox.com \
    --cc=pbs-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal