FAQ
I am looking at best practice for declaring a group of errors.

What are the pros and cons of using a string and using int to describe an
error?

in net/http:

37 // HTTP request parsing errors. 38 type ProtocolError struct { 39 ErrorString string 40 } 41 42 func (err *ProtocolError) Error() string { return err.ErrorString } 43 44 var ( 45 ErrHeaderTooLong = &ProtocolError{"header too long"} 46 ErrShortBody = &ProtocolError{"entity body too short"} 47 ErrNotSupported = &ProtocolError{"feature not supported"} 48 ErrUnexpectedTrailer = &ProtocolError{"trailer header without chunked transfer encoding"} 49 ErrMissingContentLength = &ProtocolError{"missing ContentLength in HEAD response"} 50 ErrNotMultipart = &ProtocolError{"request Content-Type isn't multipart/form-data"} 51 ErrMissingBoundary = &ProtocolError{"no multipart boundary param in Content-Type"} 52 ) 53 54 type badStringError struct { 55 what string 56 str string 57 } 58 59 func (e *badStringError) Error() string { return fmt.Sprintf("%s %q", e.what, e.str) } 60


in crypto/x509:

     15 type InvalidReason int

const ([...] 30 TooManyIntermediates 31 // IncompatibleUsage results when the certificate's key usage indicates 32 // that it may only be used for a different purpose. 33 IncompatibleUsage 34 ) 35 36 // CertificateInvalidError results when an odd error occurs. Users of this 37 // library probably want to handle all these errors uniformly. 38 type CertificateInvalidError struct { 39 Cert *Certificate 40 Reason InvalidReason 41 } 42 43 func (e CertificateInvalidError) Error() string { 44 switch e.Reason {[...] 51 case TooManyIntermediates: 52 return "x509: too many intermediates for path length constraint" 53 case IncompatibleUsage: 54 return "x509: certificate specifies an incompatible key usage" 55 } 56 return "x509: unknown error" 57 } 58


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Erwin at Jul 11, 2013 at 11:40 pm
    An int is more efficient for a computer, a string more efficient for a
    human to parse, I'd say.
    On the other hand, error strings are easy to concatenate if the error
    propagates through several layers, each layer adding a bit more context to
    them, and they then still make sense to a human.

    I've started using something like this in a few of my packages lately:

    type Error struct {
         code int
         msg string
    }

    to basically serve both needs, but it isn't ideal when such an error is
    propagated through several layers, and especially not when the error 'moves
    from package to package'.
    It would be nice if there were a small number of standard error categories
    that indicated what can possibly be done to recover from an error? One
    obvious one comes to my mind: "temporarily
    unavailable", in which case the software could retry the failed operation.



    On 11 July 2013 15:52, kugutsumen wrote:

    I am looking at best practice for declaring a group of errors.

    What are the pros and cons of using a string and using int to describe an
    error?

    in net/http:

    37 // HTTP request parsing errors. 38 type ProtocolError struct { 39 ErrorString string 40 } 41 42 func (err *ProtocolError) Error() string { return err.ErrorString } 43 44 var ( 45 ErrHeaderTooLong = &ProtocolError{"header too long"} 46 ErrShortBody = &ProtocolError{"entity body too short"} 47 ErrNotSupported = &ProtocolError{"feature not supported"} 48 ErrUnexpectedTrailer = &ProtocolError{"trailer header without chunked transfer encoding"} 49 ErrMissingContentLength = &ProtocolError{"missing ContentLength in HEAD response"} 50 ErrNotMultipart = &ProtocolError{"request Content-Type isn't multipart/form-data"} 51 ErrMissingBoundary = &ProtocolError{"no multipart boundary param in Content-Type"} 52 ) 53 54 type badStringError struct { 55 what string 56 str string 57 } 58 59 func (e *badStringError) Error() string { return fmt.Sprintf("%s %q", e.what, e.str) } 60


    in crypto/x509:

    15 type InvalidReason int

    const ([...] 30 TooManyIntermediates 31 // IncompatibleUsage results when the certificate's key usage indicates 32 // that it may only be used for a different purpose. 33 IncompatibleUsage 34 ) 35 36 // CertificateInvalidError results when an odd error occurs. Users of this 37 // library probably want to handle all these errors uniformly. 38 type CertificateInvalidError struct { 39 Cert *Certificate 40 Reason InvalidReason 41 } 42 43 func (e CertificateInvalidError) Error() string { 44 switch e.Reason {[...] 51 case TooManyIntermediates: 52 return "x509: too many intermediates for path length constraint" 53 case IncompatibleUsage: 54 return "x509: certificate specifies an incompatible key usage" 55 } 56 return "x509: unknown error" 57 } 58


    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to [email protected].
    For more options, visit https://groups.google.com/groups/opt_out.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJul 11, '13 at 1:53p
activeJul 11, '13 at 11:40p
posts2
users2
websitegolang.org

2 users in discussion

Erwin: 1 post Kugutsumen: 1 post

People

Translate

site design / logo © 2023 Grokbase