Skip to content

Commit

Permalink
Merge pull request #17 from ba-st/16-Correct-error-handling-depends-o…
Browse files Browse the repository at this point in the history
…n-the-order-they-are-installed

Install additional error handlers in Teapot server after the HTTPClientError one
  • Loading branch information
gcotelli authored Nov 22, 2018
2 parents 42336f4 + f02db72 commit 781d071
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
2 changes: 2 additions & 0 deletions source/Stargate-Examples-Tests/PetStoreAPITest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ PetStoreAPITest >> setUp [
{PetsRESTfulController new.
PetOrdersRESTfulController new}.

api on: Error addErrorHandler: [ :error :request | self fail ].

api
install;
start
Expand Down
31 changes: 16 additions & 15 deletions source/Stargate-Model/HTTPBasedRESTfulAPI.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ Class {
#superclass : #Object,
#instVars : [
'teapotServer',
'controllers'
'controllers',
'errorHandlers'
],
#category : #'Stargate-Model-Controllers'
}
Expand Down Expand Up @@ -40,18 +41,9 @@ HTTPBasedRESTfulAPI >> beCORSAwareAllowing: origins [
]

{ #category : #'private - configuring' }
HTTPBasedRESTfulAPI >> configureHttpClientErrorHandler [
HTTPBasedRESTfulAPI >> configureErrorHandlers [

teapotServer
exception:
HTTPClientError
-> [ :clientError :request |
| json |

json := NeoJSONWriter toStringPretty: clientError.
(ZnResponse statusCode: clientError code)
entity: (ZnEntity json: json);
yourself ]
errorHandlers do: [ :errorHandler | teapotServer exception: errorHandler ]
]

{ #category : #'private - configuring' }
Expand All @@ -68,21 +60,30 @@ HTTPBasedRESTfulAPI >> initializeConfiguredBy: configuration installing: aRESTfu

teapotServer := Teapot
configure: configuration , {(#notFoundHandlerClass -> Tea405AwareNotFoundHandler)}.
controllers := aRESTfulControllerCollection
controllers := aRESTfulControllerCollection.
errorHandlers := OrderedCollection new.
self
on: HTTPClientError
addErrorHandler: [ :clientError :request |
| json |
json := NeoJSONWriter toStringPretty: clientError.
(ZnResponse statusCode: clientError code)
entity: (ZnEntity json: json);
yourself ]
]

{ #category : #actions }
HTTPBasedRESTfulAPI >> install [

self
configureRoutes;
configureHttpClientErrorHandler
configureErrorHandlers
]

{ #category : #configuring }
HTTPBasedRESTfulAPI >> on: exception addErrorHandler: aDyadicBlock [

teapotServer exception: exception -> aDyadicBlock
errorHandlers add: exception -> aDyadicBlock
]

{ #category : #actions }
Expand Down

0 comments on commit 781d071

Please sign in to comment.