Output the method name as a string at runtime

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Output the method name as a string at runtime

ASAM
Hello,
I would like to output the method name as a string in case of an error.

what I've already done is this: (with thisContext selector asString)

pduDestruct
        | result |
        result := call ffiPDUDestruct.
        result = T_PDU_ERROR PDU_STATUS_NOERROR
ifFalse: [ Error signal:'Method: ' , thisContext selector asString , '  with
result: ', result item asString ]


but now I use it more often. And wanted to rebuild it like that.

initialize
        errorBlock := [ :tPduError | Error signal:'Method: ' , thisContext selector
asString , '  with result: ', tPduError item asString ].


pduDestruct
        | result |
        result := call ffiPDUDestruct.
        result = T_PDU_ERROR PDU_STATUS_NOERROR ifFalse: [ errorBlock value: result
]


but that doesn't work because "this Context selector toString" now returns
'initialize'.

Can someone give me a hint how to solve this?
Is "thisContext selector asString" the only way to get the name of the
method at runtime?

Thanks in advance.















--
Sent from: http://forum.world.st/Pharo-Smalltalk-Users-f1310670.html

Reply | Threaded
Open this post in threaded view
|

Re: Output the method name as a string at runtime

Ben Coman
Sorry for the slow response. Perhaps you've already worked it out, but anyway a few options...

You "could" pass thisContext as a block parameter

initialize
        errorBlock := [ :tPduError :aContext | Error signal:'Method: ' , aContext selector
asString , '  with result: ', tPduError item asString ].  

 pduDestruct  
          result = T_PDU_ERROR PDU_STATUS_NOERROR ifFalse: [ errorBlock value: result value: thisContext

------------------------------------
But rather I suggest a better option is to separate this logic into its own error class...

Error subclass: #PduError
    ... instanceVariables: 'result selector'

PduError class >> result: aResult context: aContext
    ^ self new initializeResult: aResult selector: aContext  selector asString

PduError >> initializeResult: aResult selector: selectorString
    result := aResult.
    selector :=  selectorString

PduEror>>messageText
    as you like - refer to other implementors for example

  PduError>>printOn:
   as you like - refer to other implementors for example


  pduDestruct
        | result |
        result := call ffiPDUDestruct.
        result = T_PDU_ERROR PDU_STATUS_NOERROR ifFalse: [ 
               (PduError result: aResult context: thisContext) signal ]

 
cheers -ben

On Sat, 27 Jun 2020 at 22:56, ASAM <[hidden email]> wrote:
Hello,
I would like to output the method name as a string in case of an error.

what I've already done is this: (with thisContext selector asString)

pduDestruct
        | result |
        result := call ffiPDUDestruct.
        result = T_PDU_ERROR PDU_STATUS_NOERROR
ifFalse: [ Error signal:'Method: ' , thisContext selector asString , '  with
result: ', result item asString ]


but now I use it more often. And wanted to rebuild it like that.

initialize
        errorBlock := [ :tPduError | Error signal:'Method: ' , thisContext selector
asString , '  with result: ', tPduError item asString ].


pduDestruct
        | result |
        result := call ffiPDUDestruct.
        result = T_PDU_ERROR PDU_STATUS_NOERROR ifFalse: [ errorBlock value: result
]


but that doesn't work because "this Context selector toString" now returns
'initialize'.

Can someone give me a hint how to solve this?
Is "thisContext selector asString" the only way to get the name of the
method at runtime?

Thanks in advance.
Reply | Threaded
Open this post in threaded view
|

Re: Output the method name as a string at runtime

ASAM
Thanks Ben,
 
in the meantime I come to a solution that looks like your second suggestion.

However, I use the already existing method    signal: withTag: .

 pduDestruct
        | result |
        result := call ffiPDUDestruct.
        result = T_PDU_ERROR PDU_STATUS_NOERROR
        ifFalse: [ DPDiagPduApError signal: thisContext selector asString withTag:
result item asString]


Although..... I like your solution almost a bit better.
What I like about your solution is, that throwing the exception (signal) is
somehow easier to see while looking.

Thanks
ASAM




--
Sent from: http://forum.world.st/Pharo-Smalltalk-Users-f1310670.html