Skip to content

Commit

Permalink
Treat ErrorCode as errors
Browse files Browse the repository at this point in the history
Also make the error messages more clear and informative
  • Loading branch information
vqhuy committed Oct 13, 2016
1 parent a5d0021 commit 8c759c4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 24 deletions.
6 changes: 3 additions & 3 deletions keyserver/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (server *ConiksServer) acceptClient(conn net.Conn, handler func(msg []byte)
func malformedClientMsg(err error) ([]byte, error) {
// check if we're just propagating a message
if err == nil {
err = ErrorMalformedClientMessage.Error()
err = ErrorMalformedClientMessage
}
response := NewErrorResponse(ErrorMalformedClientMessage)
res, e := MarshalResponse(response)
Expand All @@ -95,7 +95,7 @@ func (server *ConiksServer) makeHandler(acceptableTypes map[int]bool) func(msg [
}
if !acceptableTypes[req.Type] {
log.Printf("unacceptable message type: %q", req.Type)
return malformedClientMsg(ErrorMalformedClientMessage.Error())
return malformedClientMsg(ErrorMalformedClientMessage)
}

switch req.Type {
Expand All @@ -116,6 +116,6 @@ func (server *ConiksServer) makeHandler(acceptableTypes map[int]bool) func(msg [
if err != nil {
panic(err)
}
return res, e.Error()
return res, e
}
}
40 changes: 19 additions & 21 deletions protocol/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

package protocol

import "errors"

type ErrorCode int

const (
Expand All @@ -21,7 +19,7 @@ const (
ErrorBadSignature
ErrorBadVRFProof
ErrorBadIndex
ErrorBadMapping
ErrorBadAuthPath
ErrorBadSTR
ErrorBadCommitment
ErrorBadBinding
Expand All @@ -36,26 +34,26 @@ var ErrorResponses = map[ErrorCode]bool{
}

var (
errorMessages = map[ErrorCode]error{
Success: nil,
ErrorMalformedClientMessage: errors.New("[coniks] Malformed client request"),
ErrorNameExisted: errors.New("[coniks] Registering identity is already registered"),
ErrorNameNotFound: errors.New("[coniks] Name not found"),
ErrorDirectory: errors.New("[coniks] Directory error"),

Passed: nil,
ErrorMalformedDirectoryMessage: errors.New("[coniks] Malformed directory message"),
ErrorBadSignature: errors.New("[coniks] Directory's signature is invalid"),
ErrorBadVRFProof: errors.New("[coniks] Bad VRF proof"),
ErrorBadIndex: errors.New("[coniks] Directory returned a bad index"),
ErrorBadMapping: errors.New("[coniks] Returned name-to-key mapping is inconsistent with the root hash"),
ErrorBadSTR: errors.New("[coniks] The hash chain is inconsistent"),
ErrorBadCommitment: errors.New("[coniks] Bad commitment"),
ErrorBadBinding: errors.New("[coniks] Bad name-to-key binding"),
ErrorCouldNotVerify: errors.New("[coniks] Could not verify"),
errorMessages = map[ErrorCode]string{
Success: "[coniks] Successful client request",
ErrorMalformedClientMessage: "[coniks] Malformed client message",
ErrorNameExisted: "[coniks] Registering identity is already registered",
ErrorNameNotFound: "[coniks] Searched name not found in directory",
ErrorDirectory: "[coniks] Directory error",

Passed: "[coniks] Consistency checks passed",
ErrorMalformedDirectoryMessage: "[coniks] Malformed directory message",
ErrorBadSignature: "[coniks] Directory's signature on STR or TB is invalid",
ErrorBadVRFProof: "[coniks] Returned index is not valid for the given name",
ErrorBadIndex: "[coniks] The index in the TB and the index in the auth path do not match",
ErrorBadAuthPath: "[coniks] Returned binding is inconsistent with the tree root hash",
ErrorBadSTR: "[coniks] The hash chain is inconsistent",
ErrorBadCommitment: "[coniks] The binding commitment is invalid",
ErrorBadBinding: "[coniks] Key in the binding is inconsistent",
ErrorCouldNotVerify: "[coniks] Could not verify",
}
)

func (e ErrorCode) Error() error {
func (e ErrorCode) Error() string {
return errorMessages[e]
}

0 comments on commit 8c759c4

Please sign in to comment.