Exemple #1
1
func safeDecodeJSON(in interface{}, out interface{}) *he.Response {
	decoder, ok := in.(*json.Decoder)
	if !ok {
		ret := rfc7231.StatusUnsupportedMediaType(he.NetPrintf("PUT and POST requests must have a document media type"))
		return &ret
	}
	var tmp interface{}
	err := decoder.Decode(&tmp)
	if err != nil {
		ret := rfc7231.StatusUnsupportedMediaType(he.NetPrintf("Couldn't parse: %v", err))
		return &ret
	}
	str, err := json.Marshal(tmp)
	if err != nil {
		panic(err)
	}
	err = json.Unmarshal(str, out)
	if err != nil {
		ret := rfc7231.StatusUnsupportedMediaType(he.NetPrintf("Request body didn't have expected structure (field had wrong data type): %v", err))
		return &ret
	}
	if !jsondiff.Equal(tmp, out) {
		diff, err := jsondiff.NewJSONPatch(tmp, out)
		if err != nil {
			panic(err)
		}
		entity := decodeJSONError{
			message: locale.Sprintf("Request body didn't have expected structure (extra or missing fields).  The included diff would make the request acceptable."),
			diff:    diff,
		}
		ret := rfc7231.StatusUnsupportedMediaType(entity)
		return &ret
	}
	return nil
}
Exemple #2
1
func Logf(format string, a ...interface{}) {
	str := locale.Sprintf(format, a...).L10NString(serverMessageLocale)
	if strings.HasSuffix(format, "\n") {
		fmt.Fprint(os.Stderr, str)
	} else {
		fmt.Fprintln(os.Stderr, str)
	}
}
Exemple #3
1
// Panic, but a little nicer
func programmerError(str string) {
	panic(locale.Errorf("Programmer Error: %s", locale.Sprintf(str)))
}
Exemple #4
1
// NetPrintf is fmt.Sprintf as a NetString.
func NetPrintf(format string, a ...interface{}) NetStringer {
	return NetStringer{locale.Sprintf(format, a...)}
}
Exemple #5
1
func (e ParseError) L10NString(l locale.Spec) string {
	return locale.Sprintf("Parse Error: %s: %s", e.Header, e.Message).L10NString(l)
}