@@ -172,7 +172,13 @@ type notAllowedHandler struct {
172172func (n notAllowedHandler ) ServeHTTP (w http.ResponseWriter , r * http.Request ) {
173173 err := NewHTTPError (nil , "Method Not Allowed" , http .StatusMethodNotAllowed )
174174 w .WriteHeader (http .StatusMethodNotAllowed )
175- n .API .handleError (err , w , r )
175+
176+ contentType := defaultContentTypHeader
177+ if n .API != nil {
178+ contentType = n .API .ContentType
179+ }
180+
181+ handleError (err , w , r , contentType )
176182}
177183
178184type resource struct {
@@ -262,7 +268,7 @@ func (api *API) addResource(prototype jsonapi.MarshalIdentifier, source interfac
262268 err := res .handleIndex (c , w , r , * info )
263269 api .contextPool .Put (c )
264270 if err != nil {
265- api . handleError (err , w , r )
271+ handleError (err , w , r , api . ContentType )
266272 }
267273 })
268274
@@ -284,7 +290,7 @@ func (api *API) addResource(prototype jsonapi.MarshalIdentifier, source interfac
284290 err := res .handleRead (c , w , r , params , * info )
285291 api .contextPool .Put (c )
286292 if err != nil {
287- api . handleError (err , w , r )
293+ handleError (err , w , r , api . ContentType )
288294 }
289295 })
290296 }
@@ -303,7 +309,7 @@ func (api *API) addResource(prototype jsonapi.MarshalIdentifier, source interfac
303309 err := res .handleReadRelation (c , w , r , params , * info , relation )
304310 api .contextPool .Put (c )
305311 if err != nil {
306- api . handleError (err , w , r )
312+ handleError (err , w , r , api . ContentType )
307313 }
308314 }
309315 }(relation ))
@@ -317,7 +323,7 @@ func (api *API) addResource(prototype jsonapi.MarshalIdentifier, source interfac
317323 err := res .handleLinked (c , api , w , r , params , relation , * info )
318324 api .contextPool .Put (c )
319325 if err != nil {
320- api . handleError (err , w , r )
326+ handleError (err , w , r , api . ContentType )
321327 }
322328 }
323329 }(relation ))
@@ -330,7 +336,7 @@ func (api *API) addResource(prototype jsonapi.MarshalIdentifier, source interfac
330336 err := res .handleReplaceRelation (c , w , r , params , relation )
331337 api .contextPool .Put (c )
332338 if err != nil {
333- api . handleError (err , w , r )
339+ handleError (err , w , r , api . ContentType )
334340 }
335341 }
336342 }(relation ))
@@ -345,7 +351,7 @@ func (api *API) addResource(prototype jsonapi.MarshalIdentifier, source interfac
345351 err := res .handleAddToManyRelation (c , w , r , params , relation )
346352 api .contextPool .Put (c )
347353 if err != nil {
348- api . handleError (err , w , r )
354+ handleError (err , w , r , api . ContentType )
349355 }
350356 }
351357 }(relation ))
@@ -358,7 +364,7 @@ func (api *API) addResource(prototype jsonapi.MarshalIdentifier, source interfac
358364 err := res .handleDeleteToManyRelation (c , w , r , params , relation )
359365 api .contextPool .Put (c )
360366 if err != nil {
361- api . handleError (err , w , r )
367+ handleError (err , w , r , api . ContentType )
362368 }
363369 }
364370 }(relation ))
@@ -375,7 +381,7 @@ func (api *API) addResource(prototype jsonapi.MarshalIdentifier, source interfac
375381 err := res .handleCreate (c , w , r , info .prefix , * info )
376382 api .contextPool .Put (c )
377383 if err != nil {
378- api . handleError (err , w , r )
384+ handleError (err , w , r , api . ContentType )
379385 }
380386 })
381387 }
@@ -388,7 +394,7 @@ func (api *API) addResource(prototype jsonapi.MarshalIdentifier, source interfac
388394 err := res .handleDelete (c , w , r , params )
389395 api .contextPool .Put (c )
390396 if err != nil {
391- api . handleError (err , w , r )
397+ handleError (err , w , r , api . ContentType )
392398 }
393399 })
394400 }
@@ -402,7 +408,7 @@ func (api *API) addResource(prototype jsonapi.MarshalIdentifier, source interfac
402408 err := res .handleUpdate (c , w , r , params , * info )
403409 api .contextPool .Put (c )
404410 if err != nil {
405- api . handleError (err , w , r )
411+ handleError (err , w , r , api . ContentType )
406412 }
407413 })
408414 }
@@ -413,7 +419,6 @@ func (api *API) addResource(prototype jsonapi.MarshalIdentifier, source interfac
413419}
414420
415421func getAllowedMethods (source interface {}, collection bool ) []string {
416-
417422 result := []string {http .MethodOptions }
418423
419424 if _ , ok := source .(ResourceGetter ); ok {
@@ -597,7 +602,6 @@ func (res *resource) handleLinked(c APIContexter, api *API, w http.ResponseWrite
597602}
598603
599604func (res * resource ) handleCreate (c APIContexter , w http.ResponseWriter , r * http.Request , prefix string , info information ) error {
600-
601605 source , ok := res .source .(ResourceCreator )
602606
603607 if ! ok {
@@ -1151,16 +1155,16 @@ func replaceAttributes(query *map[string][]string, entry *jsonapi.Data) map[stri
11511155 return nil
11521156}
11531157
1154- func ( api * API ) handleError (err error , w http.ResponseWriter , r * http.Request ) {
1158+ func handleError (err error , w http.ResponseWriter , r * http.Request , contentType string ) {
11551159 log .Println (err )
11561160 if e , ok := err .(HTTPError ); ok {
1157- writeResult (w , []byte (marshalHTTPError (e )), e .status , api . ContentType )
1161+ writeResult (w , []byte (marshalHTTPError (e )), e .status , contentType )
11581162 return
11591163
11601164 }
11611165
11621166 e := NewHTTPError (err , err .Error (), http .StatusInternalServerError )
1163- writeResult (w , []byte (marshalHTTPError (e )), http .StatusInternalServerError , api . ContentType )
1167+ writeResult (w , []byte (marshalHTTPError (e )), http .StatusInternalServerError , contentType )
11641168}
11651169
11661170// TODO: this can also be replaced with a struct into that we directly json.Unmarshal
0 commit comments