Skip to content

Commit

Permalink
Extend ldap.Error to include reference to response (#272)
Browse files Browse the repository at this point in the history
* Extend ldap.Error to include reference to response

Address #271

* Just return the result

It's even more inelegant to try to wrap the limit errors and re-parse
the packet for the result.

We'll keep the most recent packet in the `Error` as a useful addition,
but we'll just return the result in any error case even though it's
counter to common Go idioms.
  • Loading branch information
johnweldon authored Jul 14, 2020
1 parent 45321a6 commit 945cdf3
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 20 deletions.
30 changes: 23 additions & 7 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ type Error struct {
ResultCode uint16
// MatchedDN is the matchedDN returned if any
MatchedDN string
// Packet is the returned packet if any
Packet *ber.Packet
}

func (e *Error) Error() string {
Expand All @@ -201,28 +203,32 @@ func GetLDAPError(packet *ber.Packet) error {
if len(packet.Children) >= 2 {
response := packet.Children[1]
if response == nil {
return &Error{ResultCode: ErrorUnexpectedResponse, Err: fmt.Errorf("Empty response in packet")}
return &Error{ResultCode: ErrorUnexpectedResponse, Err: fmt.Errorf("Empty response in packet"), Packet: packet}
}
if response.ClassType == ber.ClassApplication && response.TagType == ber.TypeConstructed && len(response.Children) >= 3 {
resultCode := uint16(response.Children[0].Value.(int64))
if resultCode == 0 { // No error
return nil
}
return &Error{ResultCode: resultCode, MatchedDN: response.Children[1].Value.(string),
Err: fmt.Errorf("%s", response.Children[2].Value.(string))}
return &Error{
ResultCode: resultCode,
MatchedDN: response.Children[1].Value.(string),
Err: fmt.Errorf("%s", response.Children[2].Value.(string)),
Packet: packet,
}
}
}

return &Error{ResultCode: ErrorNetwork, Err: fmt.Errorf("Invalid packet format")}
return &Error{ResultCode: ErrorNetwork, Err: fmt.Errorf("Invalid packet format"), Packet: packet}
}

// NewError creates an LDAP error with the given code and underlying error
func NewError(resultCode uint16, err error) error {
return &Error{ResultCode: resultCode, Err: err}
}

// IsErrorWithCode returns true if the given error is an LDAP error with the given result code
func IsErrorWithCode(err error, desiredResultCode uint16) bool {
// IsErrorAnyOf returns true if the given error is an LDAP error with any one of the given result codes
func IsErrorAnyOf(err error, codes ...uint16) bool {
if err == nil {
return false
}
Expand All @@ -232,5 +238,15 @@ func IsErrorWithCode(err error, desiredResultCode uint16) bool {
return false
}

return serverError.ResultCode == desiredResultCode
for _, code := range codes {
if serverError.ResultCode == code {
return true
}
}

return false
}
// IsErrorWithCode returns true if the given error is an LDAP error with the given result code
func IsErrorWithCode(err error, desiredResultCode uint16) bool {
return IsErrorAnyOf(err, desiredResultCode)
}
6 changes: 3 additions & 3 deletions search.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ func (l *Conn) Search(searchRequest *SearchRequest) (*SearchResult, error) {
for {
packet, err := l.readPacket(msgCtx)
if err != nil {
return nil, err
return result, err
}

switch packet.Children[1].Tag {
Expand All @@ -391,13 +391,13 @@ func (l *Conn) Search(searchRequest *SearchRequest) (*SearchResult, error) {
case 5:
err := GetLDAPError(packet)
if err != nil {
return nil, err
return result, err
}
if len(packet.Children) == 3 {
for _, child := range packet.Children[2].Children {
decodedChild, err := DecodeControl(child)
if err != nil {
return nil, fmt.Errorf("failed to decode child control: %s", err)
return result, fmt.Errorf("failed to decode child control: %w", err)
}
result.Controls = append(result.Controls, decodedChild)
}
Expand Down
30 changes: 23 additions & 7 deletions v3/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ type Error struct {
ResultCode uint16
// MatchedDN is the matchedDN returned if any
MatchedDN string
// Packet is the returned packet if any
Packet *ber.Packet
}

func (e *Error) Error() string {
Expand All @@ -201,28 +203,32 @@ func GetLDAPError(packet *ber.Packet) error {
if len(packet.Children) >= 2 {
response := packet.Children[1]
if response == nil {
return &Error{ResultCode: ErrorUnexpectedResponse, Err: fmt.Errorf("Empty response in packet")}
return &Error{ResultCode: ErrorUnexpectedResponse, Err: fmt.Errorf("Empty response in packet"), Packet: packet}
}
if response.ClassType == ber.ClassApplication && response.TagType == ber.TypeConstructed && len(response.Children) >= 3 {
resultCode := uint16(response.Children[0].Value.(int64))
if resultCode == 0 { // No error
return nil
}
return &Error{ResultCode: resultCode, MatchedDN: response.Children[1].Value.(string),
Err: fmt.Errorf("%s", response.Children[2].Value.(string))}
return &Error{
ResultCode: resultCode,
MatchedDN: response.Children[1].Value.(string),
Err: fmt.Errorf("%s", response.Children[2].Value.(string)),
Packet: packet,
}
}
}

return &Error{ResultCode: ErrorNetwork, Err: fmt.Errorf("Invalid packet format")}
return &Error{ResultCode: ErrorNetwork, Err: fmt.Errorf("Invalid packet format"), Packet: packet}
}

// NewError creates an LDAP error with the given code and underlying error
func NewError(resultCode uint16, err error) error {
return &Error{ResultCode: resultCode, Err: err}
}

// IsErrorWithCode returns true if the given error is an LDAP error with the given result code
func IsErrorWithCode(err error, desiredResultCode uint16) bool {
// IsErrorAnyOf returns true if the given error is an LDAP error with any one of the given result codes
func IsErrorAnyOf(err error, codes ...uint16) bool {
if err == nil {
return false
}
Expand All @@ -232,5 +238,15 @@ func IsErrorWithCode(err error, desiredResultCode uint16) bool {
return false
}

return serverError.ResultCode == desiredResultCode
for _, code := range codes {
if serverError.ResultCode == code {
return true
}
}

return false
}
// IsErrorWithCode returns true if the given error is an LDAP error with the given result code
func IsErrorWithCode(err error, desiredResultCode uint16) bool {
return IsErrorAnyOf(err, desiredResultCode)
}
6 changes: 3 additions & 3 deletions v3/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ func (l *Conn) Search(searchRequest *SearchRequest) (*SearchResult, error) {
for {
packet, err := l.readPacket(msgCtx)
if err != nil {
return nil, err
return result, err
}

switch packet.Children[1].Tag {
Expand All @@ -391,13 +391,13 @@ func (l *Conn) Search(searchRequest *SearchRequest) (*SearchResult, error) {
case 5:
err := GetLDAPError(packet)
if err != nil {
return nil, err
return result, err
}
if len(packet.Children) == 3 {
for _, child := range packet.Children[2].Children {
decodedChild, err := DecodeControl(child)
if err != nil {
return nil, fmt.Errorf("failed to decode child control: %s", err)
return result, fmt.Errorf("failed to decode child control: %w", err)
}
result.Controls = append(result.Controls, decodedChild)
}
Expand Down

0 comments on commit 945cdf3

Please sign in to comment.