Skip to content

Commit 185e962

Browse files
committed
better example
1 parent 231aca0 commit 185e962

1 file changed

Lines changed: 27 additions & 3 deletions

File tree

README.md

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,27 @@ spec
3232

3333
## Basic Usage
3434

35-
A proxy associates a method with a handler. You install it, enable instrumentation, run your code, then uninstall.
35+
A proxy associates a method with a handler. You install it, enable instrumentation, run your code, then uninstall. The handler decides what to do — here, `MpCountingHandler` simply counts every invocation of the wrapped method:
36+
37+
```st
38+
handler := MpCountingHandler new.
39+
p := MpMethodProxy
40+
onMethod: String >> #asUppercase
41+
handler: handler.
42+
p install.
43+
p enableInstrumentation.
44+
45+
'hello' asUppercase.
46+
'world' asUppercase.
47+
48+
p uninstall.
49+
handler count.
50+
>>> 2
51+
```
52+
53+
### Instrumenting methods that throw exceptions
54+
55+
MethodProxies can wrap **any** method, including methods that always raise an exception or perform a non-local return. The hooks fire normally and the exception keeps propagating to the caller:
3656

3757
```st
3858
handler := MpCountingHandler new.
@@ -41,13 +61,17 @@ p := MpMethodProxy
4161
handler: handler.
4262
p install.
4363
p enableInstrumentation.
44-
1 error: 'foo'.
64+
65+
[ 1 error: 'foo' ] on: Error do: [ :e | "swallow the error" ].
66+
4567
p uninstall.
4668
handler count.
4769
>>> 1
4870
```
4971

50-
A failure inside a handler will not corrupt the system. The framework safely unwinds the stack and restores normal execution:
72+
### A failing handler does not corrupt the system
73+
74+
Even when the handler itself fails, the framework safely unwinds the stack and leaves the system in a consistent state:
5175

5276
```st
5377
p := MpMethodProxy

0 commit comments

Comments
 (0)