Exception: PqcAsn1::Error

Inherits:
StandardError
  • Object
show all
Defined in:
lib/pqc_asn1.rb

Overview

Base error class for all PqcAsn1 errors.

Two attributes enable programmatic error handling without parsing the message string:

code — fine-grained symbol matching the C library status code:

:outer_sequence, :version, :algorithm, :key, :unused_bits,
:trailing_data, :pem_no_markers, :pem_label, :base64,
:invalid_oid, :extra_fields, :overflow, :alloc,
:buffer_too_small, :label_too_long, :der_parse,
:null_param, :pem_malformed

category — coarse bucket derived from code:

:malformed_input    — a DER field is structurally wrong
:malformed_encoding — Base64 / PEM boundary / label problem
:validation         — caller-side constraint violation (bad OID, overflow, etc.)
:system             — memory allocation failed

rescue PqcAsn1::ParseError => e
case e.category
when ParseError::MALFORMED_INPUT    then retry_with_lenient_parser
when ParseError::MALFORMED_ENCODING then raise "check your PEM"
end
end

Direct Known Subclasses

OIDError, ParseError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(msg = nil, code: nil, offset: nil) ⇒ Error

Returns a new instance of Error.

Parameters:

  • msg (String, nil) (defaults to: nil)

    human-readable description

  • code (Symbol, nil) (defaults to: nil)

    fine-grained error code

  • offset (Integer, nil) (defaults to: nil)

    byte offset in input where error occurred



54
55
56
57
58
# File 'lib/pqc_asn1.rb', line 54

def initialize(msg = nil, code: nil, offset: nil)
  @code = code
  @offset = offset
  super(msg)
end

Instance Attribute Details

#codeSymbol? (readonly)

Returns fine-grained machine-readable error code.

Returns:

  • (Symbol, nil)

    fine-grained machine-readable error code



44
45
46
# File 'lib/pqc_asn1.rb', line 44

def code
  @code
end

#offsetInteger? (readonly)

Returns byte offset in the input where the error was detected, or nil when the error is not parse-position-specific. Set by the C extension for Cursor and DER parse errors.

Returns:

  • (Integer, nil)

    byte offset in the input where the error was detected, or nil when the error is not parse-position-specific. Set by the C extension for Cursor and DER parse errors.



49
50
51
# File 'lib/pqc_asn1.rb', line 49

def offset
  @offset
end

Instance Method Details

#categorySymbol?

Coarse error category derived from code: :malformed_input, :malformed_encoding, :validation, or :system. Returns nil when code is nil or not categorised.

Returns:

  • (Symbol, nil)

    coarse error category derived from code: :malformed_input, :malformed_encoding, :validation, or :system. Returns nil when code is nil or not categorised.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/pqc_asn1.rb', line 63

def category
  case @code
  when :outer_sequence, :version, :algorithm, :key, :unused_bits,
       :trailing_data, :extra_fields, :der_parse
    :malformed_input
  when :pem_no_markers, :pem_label, :base64, :pem_malformed
    :malformed_encoding
  when :invalid_oid, :overflow, :buffer_too_small, :label_too_long,
       :null_param
    :validation
  when :alloc
    :system
  end
end