Improved metadirective handling
[tcl-tlc.git] / scripts / try.tcl
blob688617c26aabd4381a0c09763b23217f8ef0b357
1 # vim: ft=tcl foldmethod=marker foldmarker=<<<,>>> ts=4 shiftwidth=4
3 proc tlc::try {code args} { #<<<1
4 upvar _tlc_try_standard_actions std_actions
5 if {[info exists std_actions]} {
6 array set actions [array get std_actions]
8 array set actions $args
9 if {[set ecode [catch {uplevel $code} errmsg]]} {
10 if {$ecode != 1} {
11 return -code $ecode -errorcode $::errorCode $errmsg
13 if {[info exists actions(onerr)]} {
14 set handlers [string map {STDMSG {log error "\nUnexpected error: $errmsg\n$::errorInfo"}} $actions(onerr)]
15 array set tmp $handlers
16 if {![info exists tmp(default)]} {
17 lappend handlers default {
18 return \
19 -code 1 \
20 -errorcode $::errorCode \
21 -errorinfo $::errorInfo \
22 $errmsg
25 if {[set hcode [catch {
26 uplevel [list set errmsg $errmsg]
27 uplevel [list switch -- [lindex $::errorCode 0] $handlers]
28 } herrmsg]]} {
29 return -code $hcode -errorcode $::errorCode $herrmsg
33 if {[info exists actions(aftererr)]} {
34 if {[set ecode [catch {uplevel $actions(aftererr)} errmsg]]} {
35 return -code $ecode -errorcode $::errorCode \
36 -errorinfo $::errorInfo $errmsg
39 } else {
40 if {[info exists actions(onok)]} {
41 if {[set ecode [catch {uplevel $actions(onok)} errmsg]]} {
42 return -code $ecode -errorcode $::errorCode \
43 -errorinfo $::errorInfo $errmsg
48 if {[info exists actions(after)]} {
49 if {[set ecode [catch {uplevel $actions(after)} errmsg]]} {
50 return -code $ecode -errorcode $::errorCode \
51 -errorinfo $::errorInfo $errmsg
57 proc tlc::try_set_standard_actions {args} { #<<<1
58 upvar _tlc_try_standard_actions std_actions
59 array set std_actions $args