Skip to content

Commit

Permalink
Merge pull request #27 from juliendelplanque/26-Propose-a-debug-mode-…
Browse files Browse the repository at this point in the history
…for-the-JRPCMessageProcessor

26-Propose-a-debug-mode-for-the-JRPCMessageProcessor
  • Loading branch information
juliendelplanque authored Jan 24, 2020
2 parents 9fa590f + 37711ac commit 13ba9de
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 6 deletions.
31 changes: 25 additions & 6 deletions src/JRPC-Server-Core/JRPCMessageProcessor.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ Class {
#traits : 'JRPCParser',
#classTraits : 'JRPCParser classTrait',
#instVars : [
'handlersByName'
'handlersByName',
'debugMode'
],
#category : #'JRPC-Server-Core'
}
Expand Down Expand Up @@ -59,6 +60,16 @@ JRPCMessageProcessor >> canHandleMethodNamed: methodName [
^ handlersByName includesKey: methodName
]

{ #category : #accessing }
JRPCMessageProcessor >> debugMode [
^ debugMode
]

{ #category : #accessing }
JRPCMessageProcessor >> debugMode: anObject [
debugMode := anObject
]

{ #category : #'handling - jrpc' }
JRPCMessageProcessor >> handleJRPCNotificationObject: aJRPCNotificationObject [
"A Notification is a Request object without an 'id' member.
Expand Down Expand Up @@ -99,19 +110,26 @@ JRPCMessageProcessor >> handleJRPCRequestObject: aJRPCRequestObject [
JRPCSuccessResponseObject id: aJRPCRequestObject id result: result
]
on: Error
do: [ :jrpcError | jrpcError return: ( aJRPCRequestObject convertErrorToResponse: jrpcError ) ]
do: [ :jrpcError |
self debugMode
ifTrue: [ jrpcError pass ]
ifFalse: [ jrpcError return: ( aJRPCRequestObject convertErrorToResponse: jrpcError ) ] ]
]

{ #category : #'handling - json' }
JRPCMessageProcessor >> handleJSON: aJSONString [

"Gets aJSONString as input and returns a JSON string."
"Gets aJSONString being a request or notification serialized in JSON format as input
and returns a JSON string being the answer serialized in JSON format.
"

| jrpcResponse |

jrpcResponse := [ ( self parseSupposedJRPCMessageObjectFromString: aJSONString ) beHandledBy: self ]
on: Error
do: [ :error | error return: error asJRPCResponse ].
do: [ :error |
self debugMode
ifTrue: [ error pass ]
ifFalse: [ error return: error asJRPCResponse ] ].

^ jrpcResponse beConvertedBy: self
]
Expand Down Expand Up @@ -140,7 +158,8 @@ JRPCMessageProcessor >> handlerFor: requestOrNotification [
JRPCMessageProcessor >> initialize [

super initialize.
handlersByName := Dictionary new
handlersByName := Dictionary new.
self debugMode: false
]

{ #category : #testing }
Expand Down
10 changes: 10 additions & 0 deletions src/JRPC-Server-Core/JRPCServer.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ JRPCServer >> addHandlersFromPragmasIn: anObject [
messageProcessor addHandlersFromPragmasIn: anObject
]

{ #category : #accessing }
JRPCServer >> debugMode [
^ self messageProcessor debugMode
]

{ #category : #accessing }
JRPCServer >> debugMode: aBoolean [
^ self messageProcessor debugMode: aBoolean
]

{ #category : #'handling - jrpc' }
JRPCServer >> handleJRPCRequestObject: aJRPCRequestObject [

Expand Down
29 changes: 29 additions & 0 deletions src/JRPC-Tests/JRPCMessageProcessorTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,35 @@ Class {
#category : #'JRPC-Tests'
}

{ #category : #test }
JRPCMessageProcessorTest >> testDebugMode [
| messageProcessor |
messageProcessor := JRPCMessageProcessor new.

self deny: messageProcessor debugMode.

messageProcessor debugMode: true.

self assert: messageProcessor debugMode.
]

{ #category : #test }
JRPCMessageProcessorTest >> testDebugModeDoNotCatchError [
| messageProcessor |
messageProcessor := JRPCMessageProcessor new.
messageProcessor debugMode: true.

messageProcessor addHandlerNamed: 'error' block: [ Error signal ].

self should: [ (JRPCRequestObject id: 1 method: 'error') beHandledBy: messageProcessor ]
raise: Error.

messageProcessor debugMode: false.

self shouldnt: [ (JRPCRequestObject id: 1 method: 'error') beHandledBy: messageProcessor ]
raise: Error.
]

{ #category : #test }
JRPCMessageProcessorTest >> testHandlerFor [
| messageProcessor block handler |
Expand Down
11 changes: 11 additions & 0 deletions src/JRPC-Tests/JRPCServerTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,17 @@ JRPCServerTest >> testAddHandlersFromPragmasIn [
assert: ( self messageProcessor canHandleMethodNamed: 'stack_push' )
]

{ #category : #test }
JRPCServerTest >> testDebugMode [
self deny: server debugMode.
self deny: server messageProcessor debugMode.

server debugMode: true.

self assert: server debugMode.
self assert: server messageProcessor debugMode
]

{ #category : #tests }
JRPCServerTest >> testHandleJRPCRequestObject [
| requestForNonExistentHandler response |
Expand Down

0 comments on commit 13ba9de

Please sign in to comment.