From 68cefef5afc388618491893c7a5ad018b17979d3 Mon Sep 17 00:00:00 2001 From: Gabriel Omar Cotelli Date: Thu, 22 Nov 2018 17:38:10 -0300 Subject: [PATCH 1/2] Fixed #16 Install additional error handlers in Teapot server after the HTTPClientError one --- .../PetStoreAPITest.class.st | 2 ++ .../Stargate-Model/HTTPBasedRESTfulAPI.class.st | 17 +++++++++++++---- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/source/Stargate-Examples-Tests/PetStoreAPITest.class.st b/source/Stargate-Examples-Tests/PetStoreAPITest.class.st index a13bf22..7e748e3 100644 --- a/source/Stargate-Examples-Tests/PetStoreAPITest.class.st +++ b/source/Stargate-Examples-Tests/PetStoreAPITest.class.st @@ -55,6 +55,8 @@ PetStoreAPITest >> setUp [ {PetsRESTfulController new. PetOrdersRESTfulController new}. + api on: Error addErrorHandler: [ :error :request | self fail ]. + api install; start diff --git a/source/Stargate-Model/HTTPBasedRESTfulAPI.class.st b/source/Stargate-Model/HTTPBasedRESTfulAPI.class.st index 9c8ad64..888c225 100644 --- a/source/Stargate-Model/HTTPBasedRESTfulAPI.class.st +++ b/source/Stargate-Model/HTTPBasedRESTfulAPI.class.st @@ -6,7 +6,8 @@ Class { #superclass : #Object, #instVars : [ 'teapotServer', - 'controllers' + 'controllers', + 'errorHandlers' ], #category : #'Stargate-Model-Controllers' } @@ -39,6 +40,12 @@ HTTPBasedRESTfulAPI >> beCORSAwareAllowing: origins [ when: [ :request | request headers includesKey: 'Origin' ] ] +{ #category : #'private - configuring' } +HTTPBasedRESTfulAPI >> configureErrorHandlers [ + + errorHandlers do: [ :errorHandler | teapotServer exception: errorHandler ] +] + { #category : #'private - configuring' } HTTPBasedRESTfulAPI >> configureHttpClientErrorHandler [ @@ -68,7 +75,8 @@ HTTPBasedRESTfulAPI >> initializeConfiguredBy: configuration installing: aRESTfu teapotServer := Teapot configure: configuration , {(#notFoundHandlerClass -> Tea405AwareNotFoundHandler)}. - controllers := aRESTfulControllerCollection + controllers := aRESTfulControllerCollection. + errorHandlers := OrderedCollection new ] { #category : #actions } @@ -76,13 +84,14 @@ HTTPBasedRESTfulAPI >> install [ self configureRoutes; - configureHttpClientErrorHandler + configureHttpClientErrorHandler; + configureErrorHandlers ] { #category : #configuring } HTTPBasedRESTfulAPI >> on: exception addErrorHandler: aDyadicBlock [ - teapotServer exception: exception -> aDyadicBlock + errorHandlers add: exception -> aDyadicBlock ] { #category : #actions } From f02db72685377a5427bbb97ac8c7d8b7cf0e259d Mon Sep 17 00:00:00 2001 From: Gabriel Omar Cotelli Date: Thu, 22 Nov 2018 17:56:59 -0300 Subject: [PATCH 2/2] Account for review comments --- .../HTTPBasedRESTfulAPI.class.st | 26 +++++++------------ 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/source/Stargate-Model/HTTPBasedRESTfulAPI.class.st b/source/Stargate-Model/HTTPBasedRESTfulAPI.class.st index 888c225..5703bb2 100644 --- a/source/Stargate-Model/HTTPBasedRESTfulAPI.class.st +++ b/source/Stargate-Model/HTTPBasedRESTfulAPI.class.st @@ -46,21 +46,6 @@ HTTPBasedRESTfulAPI >> configureErrorHandlers [ errorHandlers do: [ :errorHandler | teapotServer exception: errorHandler ] ] -{ #category : #'private - configuring' } -HTTPBasedRESTfulAPI >> configureHttpClientErrorHandler [ - - teapotServer - exception: - HTTPClientError - -> [ :clientError :request | - | json | - - json := NeoJSONWriter toStringPretty: clientError. - (ZnResponse statusCode: clientError code) - entity: (ZnEntity json: json); - yourself ] -] - { #category : #'private - configuring' } HTTPBasedRESTfulAPI >> configureRoutes [ @@ -76,7 +61,15 @@ HTTPBasedRESTfulAPI >> initializeConfiguredBy: configuration installing: aRESTfu teapotServer := Teapot configure: configuration , {(#notFoundHandlerClass -> Tea405AwareNotFoundHandler)}. controllers := aRESTfulControllerCollection. - errorHandlers := OrderedCollection new + 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 } @@ -84,7 +77,6 @@ HTTPBasedRESTfulAPI >> install [ self configureRoutes; - configureHttpClientErrorHandler; configureErrorHandlers ]