Skip to content

Commit

Permalink
Define a StripeError struct and send this on authentication errors
Browse files Browse the repository at this point in the history
  • Loading branch information
remi-stripe committed Nov 12, 2017
1 parent 98a9cc4 commit da8c896
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ const (
"Authorization was '%s'."
)

type ErrorInfo struct {
Type string `json:"type"`
Message string `json:"message"`
}

type ResponseError struct {
ErrorInfo ErrorInfo `json:"error"`
}

// ExpansionLevel represents expansions on a single "level" of resource. It may
// have subexpansions that are meant to take effect on resources that are
// nested below it (on other levels).
Expand Down Expand Up @@ -86,10 +95,17 @@ func (s *StubServer) HandleRequest(w http.ResponseWriter, r *http.Request) {
start := time.Now()
fmt.Printf("Request: %v %v\n", r.Method, r.URL.Path)

// Prepare error response as needed for all failures
stripeError := &ResponseError{
ErrorInfo: ErrorInfo{
Type: "invalid_request_error",
},
}

auth := r.Header.Get("Authorization")
if !validateAuth(auth) {
writeResponse(w, r, start, http.StatusUnauthorized,
fmt.Sprintf(invalidAuthorization, auth))
stripeError.ErrorInfo.Message = fmt.Sprintf(invalidAuthorization, auth)
writeResponse(w, r, start, http.StatusUnauthorized, stripeError)
return
}

Expand Down

0 comments on commit da8c896

Please sign in to comment.