1
#pragma once
2

            
3
#include "envoy/api/io_error.h"
4

            
5
#include "source/common/common/assert.h"
6

            
7
namespace Envoy {
8
namespace Network {
9

            
10
class IoSocketError : public Api::IoError {
11
public:
12
  static Api::IoErrorPtr create(int sys_errno);
13
619
  ~IoSocketError() override = default;
14

            
15
  Api::IoError::IoErrorCode getErrorCode() const override;
16
  std::string getErrorDetails() const override;
17
485
  int getSystemErrorCode() const override { return errno_; }
18

            
19
  // IoErrorCode::Again is used frequently. This custom error returns a
20
  // reusable singleton to avoid repeated allocation.
21
  static Api::IoErrorPtr getIoSocketEagainError();
22
  static Api::IoErrorPtr getIoSocketEbadfError();
23

            
24
  // This error is introduced when Envoy create socket for unsupported address. It is either a bug,
25
  // or this Envoy instance received config which is not yet supported. This should not be fatal
26
  // error.
27
  static Api::IoCallUint64Result ioResultSocketInvalidAddress();
28

            
29
private:
30
  explicit IoSocketError(int sys_errno)
31
581
      : errno_(sys_errno), error_code_(errorCodeFromErrno(errno_)) {
32
581
    ASSERT(error_code_ != IoErrorCode::Again,
33
581
           "Didn't use getIoSocketEagainError() to generate `Again`.");
34
581
  }
35
  explicit IoSocketError(int sys_errno, Api::IoError::IoErrorCode error_code)
36
521
      : errno_(sys_errno), error_code_(error_code) {}
37

            
38
  static Api::IoError::IoErrorCode errorCodeFromErrno(int sys_errno);
39

            
40
  static Api::IoErrorPtr getIoSocketInvalidAddressError();
41

            
42
  const int errno_;
43
  const Api::IoError::IoErrorCode error_code_;
44
};
45

            
46
} // namespace Network
47
} // namespace Envoy