Skip to content

Commit

Permalink
Merge pull request #44 from gcotelli/allow_to_configure_error_handlers
Browse files Browse the repository at this point in the history
Allow to configure error handlers in JRPCMessageProcessor
  • Loading branch information
gcotelli authored Dec 20, 2021
2 parents cceafbb + f6c6a1f commit 895e239
Show file tree
Hide file tree
Showing 17 changed files with 279 additions and 41 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/loading-groups.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Baseline Groups

on: [push,pull_request,workflow_dispatch]

jobs:
group-loading:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
smalltalk: [ Pharo64-9.0, Pharo64-8.0 ]
load-spec: [ client-deployment, server-deployment, http-transport, tcp-transport, server-logging ]
name: ${{ matrix.smalltalk }} + ${{ matrix.load-spec }}
steps:
- uses: actions/checkout@v2
- uses: hpi-swa/setup-smalltalkCI@v1
with:
smalltalk-image: ${{ matrix.smalltalk }}
- name: Load group in image
run: smalltalkci -s ${{ matrix.smalltalk }} .smalltalkci/.loading.${{ matrix.load-spec }}.ston
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
timeout-minutes: 15
26 changes: 26 additions & 0 deletions .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Unit Tests

on: [push,pull_request,workflow_dispatch]

jobs:
unit-tests:
runs-on: ubuntu-latest
strategy:
matrix:
smalltalk: [ Pharo64-9.0, Pharo64-8.0, Pharo32-8.0, Pharo64-7.0, Pharo32-7.0 , Pharo32-6.1 ]
name: ${{ matrix.smalltalk }}
steps:
- uses: actions/checkout@v2
- uses: hpi-swa/setup-smalltalkCI@v1
with:
smalltalk-image: ${{ matrix.smalltalk }}
- name: Load Image and Run Tests
run: smalltalkci -s ${{ matrix.smalltalk }} .smalltalkci/.unit-tests.ston
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
timeout-minutes: 15
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v1
with:
name: ${{matrix.os}}-${{matrix.smalltalk}}
token: ${{ secrets.CODECOV_TOKEN }}
13 changes: 13 additions & 0 deletions .smalltalkci/.loading.client-deployment.ston
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
SmalltalkCISpec {
#loading : [
SCIMetacelloLoadSpec {
#baseline : 'JRPC',
#directory : '../src',
#load : [ 'Client-Deployment' ],
#platforms : [ #pharo ]
}
],
#testing : {
#failOnZeroTests : false
}
}
13 changes: 13 additions & 0 deletions .smalltalkci/.loading.http-transport.ston
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
SmalltalkCISpec {
#loading : [
SCIMetacelloLoadSpec {
#baseline : 'JRPC',
#directory : '../src',
#load : [ 'HTTP-Transport' ],
#platforms : [ #pharo ]
}
],
#testing : {
#failOnZeroTests : false
}
}
13 changes: 13 additions & 0 deletions .smalltalkci/.loading.server-deployment.ston
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
SmalltalkCISpec {
#loading : [
SCIMetacelloLoadSpec {
#baseline : 'JRPC',
#directory : '../src',
#load : [ 'Server-Deployment' ],
#platforms : [ #pharo ]
}
],
#testing : {
#failOnZeroTests : false
}
}
13 changes: 13 additions & 0 deletions .smalltalkci/.loading.server-logging.ston
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
SmalltalkCISpec {
#loading : [
SCIMetacelloLoadSpec {
#baseline : 'JRPC',
#directory : '../src',
#load : [ 'Server-Logging' ],
#platforms : [ #pharo ]
}
],
#testing : {
#failOnZeroTests : false
}
}
13 changes: 13 additions & 0 deletions .smalltalkci/.loading.tcp-transport.ston
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
SmalltalkCISpec {
#loading : [
SCIMetacelloLoadSpec {
#baseline : 'JRPC',
#directory : '../src',
#load : [ 'TCP-Transport' ],
#platforms : [ #pharo ]
}
],
#testing : {
#failOnZeroTests : false
}
}
7 changes: 4 additions & 3 deletions .smalltalk.ston → .smalltalkci/.unit-tests.ston
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ SmalltalkCISpec {
#loading : [
SCIMetacelloLoadSpec {
#baseline : 'JRPC',
#directory : 'src',
#directory : '../src',
#load : [ 'CI' ],
#platforms : [ #pharo ]
#platforms : [ #pharo ]
}
],
#testing : {
#coverage : {
#packages : [ 'JRPC-Client' , 'JRPC-Server*' , 'JRPC-Tests' ]
#packages : [ 'JRPC-Client' , 'JRPC-Server*' , 'JRPC-Tests' ],
#format: #lcov
}
}
}
20 changes: 0 additions & 20 deletions .travis.yml

This file was deleted.

2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2018-2019 Julien Delplanque
Copyright (c) 2018-2021 Julien Delplanque

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# JRPC
[![Build Status](https://travis-ci.org/juliendelplanque/JRPC.svg?branch=master)](https://travis-ci.org/juliendelplanque/JRPC)
[![Coverage Status](https://coveralls.io/repos/github/juliendelplanque/JRPC/badge.svg?branch=master)](https://coveralls.io/github/juliendelplanque/JRPC?branch=master)

[![Unit Tests](https://github.com/juliendelplanque/JRPC/actions/workflows/unit-tests.yml/badge.svg)](https://github.com/juliendelplanque/JRPC/actions/workflows/unit-tests.yml)
[![Coverage Status](https://codecov.io/github/juliendelplanque/JRPC/coverage.svg?branch=master)](https://codecov.io/gh/juliendelplanque/JRPC/branch/master)
[![Baseline Groups](https://github.com/juliendelplanque/JRPC/actions/workflows/loading-groups.yml/badge.svg)](https://github.com/juliendelplanque/JRPC/actions/workflows/loading-groups.yml)

[![License](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)
[![Pharo version](https://img.shields.io/badge/Pharo-6.1-%23aac9ff.svg)](https://pharo.org/download)
[![Pharo version](https://img.shields.io/badge/Pharo-7.0-%23aac9ff.svg)](https://pharo.org/download)
Expand Down
6 changes: 6 additions & 0 deletions src/JRPC-Server-Core/JRPCAbstractMessageProcessor.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ Class {
#category : #'JRPC-Server-Core'
}

{ #category : #'handlers management' }
JRPCAbstractMessageProcessor >> addErrorHandler: aBlock [

self subclassResponsibility
]

{ #category : #'handlers management' }
JRPCAbstractMessageProcessor >> addHandler: aJRPCHandler [
"Adds aJRPCHandler to the list of available handlers."
Expand Down
50 changes: 35 additions & 15 deletions src/JRPC-Server-Core/JRPCMessageProcessor.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,18 @@ Class {
#classTraits : 'JRPCParser classTrait',
#instVars : [
'handlers',
'debugMode'
'debugMode',
'errorHandlers'
],
#category : #'JRPC-Server-Core'
}

{ #category : #'handlers management' }
JRPCMessageProcessor >> addErrorHandler: aBlock [

errorHandlers add: aBlock
]

{ #category : #'handlers management' }
JRPCMessageProcessor >> addHandler: aJRPCHandler [

Expand All @@ -32,8 +39,22 @@ JRPCMessageProcessor >> debugMode [
]

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

{ #category : #private }
JRPCMessageProcessor >> handleErrorsDuring: aBlock for: aJRPCRequestObject [

^ self debugMode
ifTrue: aBlock
ifFalse: [
aBlock
on: Error
do: [ :jrpcError |
errorHandlers do: [ :handler | handler cull: jrpcError ].
jrpcError return:
(aJRPCRequestObject convertErrorToResponse: jrpcError) ] ]
]

{ #category : #'handling - jrpc' }
Expand Down Expand Up @@ -68,18 +89,16 @@ JRPCMessageProcessor >> handleJRPCNotificationObject: aJRPCNotificationObject [
{ #category : #'handling - jrpc' }
JRPCMessageProcessor >> handleJRPCRequestObject: aJRPCRequestObject [

^ [ | handler result |

handler := self handlerFor: aJRPCRequestObject.
handler checkParametersForRequest: aJRPCRequestObject.
result := handler executeWithArguments: aJRPCRequestObject params.
JRPCSuccessResponseObject id: aJRPCRequestObject id result: result
]
on: Error
do: [ :jrpcError |
self debugMode
ifTrue: [ jrpcError pass ]
ifFalse: [ jrpcError return: ( aJRPCRequestObject convertErrorToResponse: jrpcError ) ] ]
^ self
handleErrorsDuring: [
| handler result |
handler := self handlerFor: aJRPCRequestObject.
handler checkParametersForRequest: aJRPCRequestObject.
result := handler executeWithArguments: aJRPCRequestObject params.
JRPCSuccessResponseObject
id: aJRPCRequestObject id
result: result ]
for: aJRPCRequestObject
]

{ #category : #'handling - json' }
Expand Down Expand Up @@ -128,6 +147,7 @@ JRPCMessageProcessor >> initialize [

super initialize.
handlers := OrderedCollection new.
errorHandlers := OrderedCollection new.
self debugMode: false
]

Expand Down
6 changes: 6 additions & 0 deletions src/JRPC-Server-Core/JRPCMessageProcessorDecorator.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ JRPCMessageProcessorDecorator class >> decorate: aMessageProcessor [
yourself
]

{ #category : #adding }
JRPCMessageProcessorDecorator >> addErrorHandler: aBlock [

self decoratedMessageProcessor addErrorHandler: aBlock
]

{ #category : #adding }
JRPCMessageProcessorDecorator >> addHandler: aJRPCHandler [
self decoratedMessageProcessor addHandler: aJRPCHandler
Expand Down
6 changes: 6 additions & 0 deletions src/JRPC-Server-Core/JRPCServer.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ Class {
#category : #'JRPC-Server-Core'
}

{ #category : #'handlers management' }
JRPCServer >> addErrorHandler: anErrorHandler [

self messageProcessor addErrorHandler: anErrorHandler
]

{ #category : #'handlers management' }
JRPCServer >> addHandler: aJRPCHandler [

Expand Down
82 changes: 82 additions & 0 deletions src/JRPC-Tests/JRPCMessageProcessorTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,65 @@ JRPCMessageProcessorTest >> testDebugModeDoNotCatchError [
raise: Error.
]

{ #category : #'tests - error handlers' }
JRPCMessageProcessorTest >> testErrorHandlersAreNotEvaluatedOnSuccess [

| messageProcessor response |
messageProcessor := JRPCMessageProcessor new.
messageProcessor
addHandlerNamed: 'success' block: [ 'Sucess!' ];
addErrorHandler: [ :error | self fail ].

response := messageProcessor handleJRPCRequestObject:
(JRPCRequestObject new
id: 1;
method: 'success';
yourself).

self deny: response isError
]

{ #category : #'tests - error handlers' }
JRPCMessageProcessorTest >> testErrorHandlersAreNotEvaluatedWhenDebugModeIsEnabled [

| messageProcessor |
messageProcessor := JRPCMessageProcessor new.
messageProcessor
debugMode: true;
addHandlerNamed: 'error' block: [ 1 / 0 ];
addErrorHandler: [ :error | self fail ].

self
should: [
messageProcessor handleJRPCRequestObject: (JRPCRequestObject new
id: 1;
method: 'error';
yourself) ]
raise: ZeroDivide
]

{ #category : #'tests - error handlers' }
JRPCMessageProcessorTest >> testErrorHandling [

| messageProcessor errorWasRaised response |
errorWasRaised := false.

messageProcessor := JRPCMessageProcessor new.
messageProcessor
addHandlerNamed: 'error' block: [ Error signal ];
addErrorHandler: [ :error | errorWasRaised := true ].

response := messageProcessor handleJRPCRequestObject:
(JRPCRequestObject new
id: 1;
method: 'error';
yourself).

self
assert: response isError;
assert: errorWasRaised
]

{ #category : #test }
JRPCMessageProcessorTest >> testHandlerFor [
| messageProcessor block handler |
Expand Down Expand Up @@ -196,3 +255,26 @@ JRPCMessageProcessorTest >> testRemoveHandler [
self assert: messageProcessor handlers size equals: 0.

]

{ #category : #'tests - error handlers' }
JRPCMessageProcessorTest >> testSeveralErrorHandlers [

| messageProcessor response errorCount |
errorCount := 0.

messageProcessor := JRPCMessageProcessor new.
messageProcessor
addHandlerNamed: 'error' block: [ Error signal ];
addErrorHandler: [ :error | errorCount := errorCount + 1 ];
addErrorHandler: [ :error | errorCount := errorCount + 1 ].

response := messageProcessor handleJRPCRequestObject:
(JRPCRequestObject new
id: 1;
method: 'error';
yourself).

self
assert: response isError;
assert: errorCount equals: 2
]
Loading

0 comments on commit 895e239

Please sign in to comment.