Skip to content

Commit

Permalink
Improve CORS handling for requests responded with errors
Browse files Browse the repository at this point in the history
Use the available CORS configuration to set the expected headers in case the HTTPClientError handler kicked-in.
Fix the global error handler configured in StargateApplication to pay attention to CORS.
Improve tests related to CORS.

Fixes #180
  • Loading branch information
gcotelli committed Nov 22, 2023
1 parent a1ca293 commit f52538e
Show file tree
Hide file tree
Showing 72 changed files with 1,380 additions and 899 deletions.
83 changes: 43 additions & 40 deletions source/Stargate-API-Skeleton/StargateApplication.class.st
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
Class {
#name : #StargateApplication,
#superclass : #LaunchpadApplication,
#name : 'StargateApplication',
#superclass : 'LaunchpadApplication',
#instVars : [
'apiOptional'
],
#classInstVars : [
'Version'
],
#category : #'Stargate-API-Skeleton'
#category : 'Stargate-API-Skeleton',
#package : 'Stargate-API-Skeleton'
}

{ #category : #accessing }
{ #category : 'accessing' }
StargateApplication class >> configurationParameters [

^ self stargateConfigurationParameters
]

{ #category : #private }
{ #category : 'private' }
StargateApplication class >> fileReferenceToDumpStackTrace [

| logsDirectory stackTraceDumpFile |
Expand Down Expand Up @@ -46,44 +47,44 @@ StargateApplication class >> fileReferenceToDumpStackTrace [
^ stackTraceDumpFile
]

{ #category : #initialization }
{ #category : 'initialization' }
StargateApplication class >> initializeVersion [

<ignoreForCoverage>
Version := VersionFromRepositoryResolver new valueFor: self projectName
]

{ #category : #testing }
{ #category : 'testing' }
StargateApplication class >> isAbstract [

^ self = StargateApplication
]

{ #category : #private }
{ #category : 'private' }
StargateApplication class >> logsDirectory [

^ ( FileLocator workingDirectory / 'logs' ) ensureCreateDirectory
]

{ #category : #private }
{ #category : 'private' }
StargateApplication class >> projectName [

^ self subclassResponsibility
]

{ #category : #private }
{ #category : 'private' }
StargateApplication class >> sectionsForStargateConfiguration [

^ #('Stargate')
]

{ #category : #private }
{ #category : 'private' }
StargateApplication class >> stackTraceDumpExtension [

^ 'dump'
]

{ #category : #private }
{ #category : 'private' }
StargateApplication class >> stargateConfigurationParameters [

^ Array
Expand All @@ -101,13 +102,13 @@ StargateApplication class >> stargateConfigurationParameters [
convertingWith: #asByteArray ) asSensitive
]

{ #category : #accessing }
{ #category : 'accessing' }
StargateApplication class >> version [

^ Version
]

{ #category : #'private - accessing' }
{ #category : 'private - accessing' }
StargateApplication >> apiConfiguration [

^ Array with: #serverUrl -> self stargateConfiguration publicURL
Expand All @@ -116,7 +117,7 @@ StargateApplication >> apiConfiguration [
with: #operations -> self operationsConfiguration
]

{ #category : #'private - accessing' }
{ #category : 'private - accessing' }
StargateApplication >> applicationConfigurationConfiguration [

^ Dictionary new
Expand All @@ -126,15 +127,15 @@ StargateApplication >> applicationConfigurationConfiguration [
yourself
]

{ #category : #'private - accessing' }
{ #category : 'private - accessing' }
StargateApplication >> applicationControlCommandsToEnable [

"This method must return an array of application control command names to be enabled"

^ #( )
]

{ #category : #'private - accessing' }
{ #category : 'private - accessing' }
StargateApplication >> applicationControlConfiguration [

^ Dictionary new
Expand All @@ -143,7 +144,7 @@ StargateApplication >> applicationControlConfiguration [
yourself
]

{ #category : #'private - accessing' }
{ #category : 'private - accessing' }
StargateApplication >> applicationInfoConfiguration [

^ Dictionary new
Expand All @@ -152,15 +153,15 @@ StargateApplication >> applicationInfoConfiguration [
yourself
]

{ #category : #'private - accessing' }
{ #category : 'private - accessing' }
StargateApplication >> authAlgorithm [
"The signing algorithm identifier as defined in https://datatracker.ietf.org/doc/html/rfc7518#section-3.1.
Used for securing operational plugins, by default HMAC using SHA-256"

^ 'HS256'
]

{ #category : #'private - activation/deactivation' }
{ #category : 'private - activation/deactivation' }
StargateApplication >> basicStartWithin: context [

| api |
Expand All @@ -173,41 +174,43 @@ StargateApplication >> basicStartWithin: context [
apiOptional := Optional containing: api
]

{ #category : #'private - activation/deactivation' }
{ #category : 'private - activation/deactivation' }
StargateApplication >> basicStop [

apiOptional withContentDo: [ :api | api stop ].
super basicStop
]

{ #category : #'private - activation/deactivation' }
{ #category : 'private - activation/deactivation' }
StargateApplication >> configureGlobalErrorHandlerIn: api [

[
LogRecord emitInfo: 'Configuring error handlers' during: [
api
on: Error addErrorHandler: [ :error |
on: Error addErrorHandler: [ :error :request |
| response |

LogRecord emitError: ( 'Unexpected error: "<1s>"' expandMacrosWith: error messageText ).
self stackTraceDumper dumpStackTraceFor: error.
TeaResponse serverError
body: error messageText;
yourself
response := ZnResponse serverError: error messageText.
api applyCrossSharingResourceConfigurationTo: response accordingTo: request.
response
];
on: Exit addErrorHandler: [ :exit |
exit isSuccess then: [ self stop ].
exit pass
]
exit isSuccess then: [ self stop ].
exit pass
]
]
] unless: self isDebugModeEnabled
]

{ #category : #'private - accessing' }
{ #category : 'private - accessing' }
StargateApplication >> controllersToInstall [

^ self subclassResponsibility
]

{ #category : #'private - activation/deactivation' }
{ #category : 'private - activation/deactivation' }
StargateApplication >> createAPI [

| api |
Expand All @@ -218,22 +221,22 @@ StargateApplication >> createAPI [
^ api
]

{ #category : #initialization }
{ #category : 'initialization' }
StargateApplication >> initialize [

super initialize.
apiOptional := Optional unused
]

{ #category : #'private - activation/deactivation' }
{ #category : 'private - activation/deactivation' }
StargateApplication >> installAndStart: api [

LogRecord
emitInfo: 'Installing API' during: [ api install ];
emitInfo: 'Starting API' during: [ api start ]
]

{ #category : #'private - activation/deactivation' }
{ #category : 'private - activation/deactivation' }
StargateApplication >> logAPIVersion [

BasicApplicationInformationProvider new applicationNamed: self class commandName
Expand All @@ -242,15 +245,15 @@ StargateApplication >> logAPIVersion [
LogRecord emitInfo: ( 'API Version: <1s>' expandMacrosWith: self class version )
]

{ #category : #'private - accessing' }
{ #category : 'private - accessing' }
StargateApplication >> loggersConfiguration [

^ Dictionary new
at: #enabled put: true;
yourself
]

{ #category : #'private - accessing' }
{ #category : 'private - accessing' }
StargateApplication >> metricsConfiguration [

^ Dictionary new
Expand All @@ -262,7 +265,7 @@ StargateApplication >> metricsConfiguration [
yourself
]

{ #category : #'private - accessing' }
{ #category : 'private - accessing' }
StargateApplication >> operationsConfiguration [

^ Dictionary new
Expand All @@ -277,7 +280,7 @@ StargateApplication >> operationsConfiguration [
yourself
]

{ #category : #'error handling' }
{ #category : 'error handling' }
StargateApplication >> stackTraceDumper [

^ StackTraceTextDumper on: [ :dumpAction |
Expand All @@ -286,7 +289,7 @@ StargateApplication >> stackTraceDumper [
]
]

{ #category : #'private - accessing' }
{ #category : 'private - accessing' }
StargateApplication >> stargateConfiguration [

^ self configuration stargate
Expand Down
2 changes: 1 addition & 1 deletion source/Stargate-API-Skeleton/package.st
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Package { #name : #'Stargate-API-Skeleton' }
Package { #name : 'Stargate-API-Skeleton' }
Loading

0 comments on commit f52538e

Please sign in to comment.