...

Source file src/github.com/ory/fosite/access_error.go

Documentation: github.com/ory/fosite

     1  /*
     2   * Copyright © 2015-2018 Aeneas Rekkas <aeneas+oss@aeneas.io>
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   *
    16   * @author		Aeneas Rekkas <aeneas+oss@aeneas.io>
    17   * @copyright 	2015-2018 Aeneas Rekkas <aeneas+oss@aeneas.io>
    18   * @license 	Apache-2.0
    19   *
    20   */
    21  
    22  package fosite
    23  
    24  import (
    25  	"encoding/json"
    26  	"fmt"
    27  	"net/http"
    28  )
    29  
    30  func (f *Fosite) WriteAccessError(rw http.ResponseWriter, req AccessRequester, err error) {
    31  	f.writeJsonError(rw, req, err)
    32  }
    33  
    34  func (f *Fosite) writeJsonError(rw http.ResponseWriter, requester AccessRequester, err error) {
    35  	rw.Header().Set("Content-Type", "application/json;charset=UTF-8")
    36  	rw.Header().Set("Cache-Control", "no-store")
    37  	rw.Header().Set("Pragma", "no-cache")
    38  
    39  	rfcerr := ErrorToRFC6749Error(err).WithLegacyFormat(f.UseLegacyErrorFormat).WithExposeDebug(f.SendDebugMessagesToClients)
    40  
    41  	if requester != nil {
    42  		rfcerr = rfcerr.WithLocalizer(f.MessageCatalog, getLangFromRequester(requester))
    43  	}
    44  
    45  	js, err := json.Marshal(rfcerr)
    46  	if err != nil {
    47  		if f.SendDebugMessagesToClients {
    48  			errorMessage := EscapeJSONString(err.Error())
    49  			http.Error(rw, fmt.Sprintf(`{"error":"server_error","error_description":"%s"}`, errorMessage), http.StatusInternalServerError)
    50  		} else {
    51  			http.Error(rw, `{"error":"server_error"}`, http.StatusInternalServerError)
    52  		}
    53  		return
    54  	}
    55  
    56  	rw.WriteHeader(rfcerr.CodeField)
    57  	// ignoring the error because the connection is broken when it happens
    58  	_, _ = rw.Write(js)
    59  }
    60  

View as plain text