...

Source file src/github.com/letsencrypt/boulder/web/probs.go

Documentation: github.com/letsencrypt/boulder/web

     1  package web
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  
     7  	berrors "github.com/letsencrypt/boulder/errors"
     8  	"github.com/letsencrypt/boulder/probs"
     9  )
    10  
    11  func problemDetailsForBoulderError(err *berrors.BoulderError, msg string) *probs.ProblemDetails {
    12  	var outProb *probs.ProblemDetails
    13  
    14  	switch err.Type {
    15  	case berrors.Malformed:
    16  		outProb = probs.Malformed(fmt.Sprintf("%s :: %s", msg, err))
    17  	case berrors.Unauthorized:
    18  		outProb = probs.Unauthorized(fmt.Sprintf("%s :: %s", msg, err))
    19  	case berrors.NotFound:
    20  		outProb = probs.NotFound(fmt.Sprintf("%s :: %s", msg, err))
    21  	case berrors.RateLimit:
    22  		outProb = probs.RateLimited(fmt.Sprintf("%s :: %s", msg, err))
    23  	case berrors.InternalServer:
    24  		// Internal server error messages may include sensitive data, so we do
    25  		// not include it.
    26  		outProb = probs.ServerInternal(msg)
    27  	case berrors.RejectedIdentifier:
    28  		outProb = probs.RejectedIdentifier(fmt.Sprintf("%s :: %s", msg, err))
    29  	case berrors.InvalidEmail:
    30  		outProb = probs.InvalidContact(fmt.Sprintf("%s :: %s", msg, err))
    31  	case berrors.CAA:
    32  		outProb = probs.CAA(fmt.Sprintf("%s :: %s", msg, err))
    33  	case berrors.MissingSCTs:
    34  		// MissingSCTs are an internal server error, but with a specific error
    35  		// message related to the SCT problem
    36  		outProb = probs.ServerInternal(fmt.Sprintf("%s :: %s", msg, "Unable to meet CA SCT embedding requirements"))
    37  	case berrors.OrderNotReady:
    38  		outProb = probs.OrderNotReady(fmt.Sprintf("%s :: %s", msg, err))
    39  	case berrors.BadPublicKey:
    40  		outProb = probs.BadPublicKey(fmt.Sprintf("%s :: %s", msg, err))
    41  	case berrors.BadCSR:
    42  		outProb = probs.BadCSR(fmt.Sprintf("%s :: %s", msg, err))
    43  	case berrors.AlreadyRevoked:
    44  		outProb = probs.AlreadyRevoked(fmt.Sprintf("%s :: %s", msg, err))
    45  	case berrors.BadRevocationReason:
    46  		outProb = probs.BadRevocationReason(fmt.Sprintf("%s :: %s", msg, err))
    47  	case berrors.UnsupportedContact:
    48  		outProb = probs.UnsupportedContact(fmt.Sprintf("%s :: %s", msg, err))
    49  	default:
    50  		// Internal server error messages may include sensitive data, so we do
    51  		// not include it.
    52  		outProb = probs.ServerInternal(msg)
    53  	}
    54  
    55  	if len(err.SubErrors) > 0 {
    56  		var subProbs []probs.SubProblemDetails
    57  		for _, subErr := range err.SubErrors {
    58  			subProbs = append(subProbs, subProblemDetailsForSubError(subErr, msg))
    59  		}
    60  		return outProb.WithSubProblems(subProbs)
    61  	}
    62  
    63  	return outProb
    64  }
    65  
    66  // ProblemDetailsForError turns an error into a ProblemDetails with the special
    67  // case of returning the same error back if its already a ProblemDetails. If the
    68  // error is of an type unknown to ProblemDetailsForError, it will return a
    69  // ServerInternal ProblemDetails.
    70  func ProblemDetailsForError(err error, msg string) *probs.ProblemDetails {
    71  	var probsProblemDetails *probs.ProblemDetails
    72  	var berrorsBoulderError *berrors.BoulderError
    73  	if errors.As(err, &probsProblemDetails) {
    74  		return probsProblemDetails
    75  	} else if errors.As(err, &berrorsBoulderError) {
    76  		return problemDetailsForBoulderError(berrorsBoulderError, msg)
    77  	} else {
    78  		// Internal server error messages may include sensitive data, so we do
    79  		// not include it.
    80  		return probs.ServerInternal(msg)
    81  	}
    82  }
    83  
    84  // subProblemDetailsForSubError converts a SubBoulderError into
    85  // a SubProblemDetails using problemDetailsForBoulderError.
    86  func subProblemDetailsForSubError(subErr berrors.SubBoulderError, msg string) probs.SubProblemDetails {
    87  	return probs.SubProblemDetails{
    88  		Identifier:     subErr.Identifier,
    89  		ProblemDetails: *problemDetailsForBoulderError(subErr.BoulderError, msg),
    90  	}
    91  }
    92  

View as plain text