Better error handling.

This commit is contained in:
Nuno Cruces 2022-05-06 13:48:31 +01:00
parent 3a6fb3e70e
commit 2e4849398a
11 changed files with 94 additions and 29 deletions

View file

@ -405,10 +405,12 @@ func loadFlags() []zenity.Option {
ico = zenity.WarningIcon
case "dialog-password":
ico = zenity.PasswordIcon
default:
case unspecified:
ico = zenity.NoIcon
default:
opts = append(opts, zenity.CustomIcon(icon))
}
if icon != unspecified {
if ico != 0 {
opts = append(opts, ico)
}

View file

@ -1,6 +1,8 @@
package zenity
import (
"os"
"github.com/ncruces/zenity/internal/zenutil"
)
@ -13,6 +15,10 @@ func entry(text string, opts options) (string, error) {
data.Options.Hidden = opts.hideText
data.Options.Timeout = zenutil.Timeout
if opts.customIcon != "" {
_, err := os.Stat(opts.customIcon)
if err != nil {
return "", err
}
data.IconPath = opts.customIcon
} else {
data.Options.Icon = opts.icon.String()

View file

@ -16,7 +16,11 @@ var scripts = template.Must(template.New("").Funcs(template.FuncMap{"json": func
var app=Application.currentApplication()
app.includeStandardAdditions=true
app.activate()
var res=app.chooseColor({defaultColor:{{json .}}})
ObjC.import('stdio')
ObjC.import('stdlib')
try{var res=app.chooseColor({defaultColor:{{json .}}})}catch(e){if(e.errorNumber===-128)$.exit(1)
$.dprintf(2,e)
$.exit(-1)}
{'rgb('+res.map(x=>Math.round(x*255))+')'}
{{- end}}
{{define "date" -}}
@ -48,7 +52,8 @@ var res=alert.runModal
switch(res){case $.NSAlertThirdButtonReturn:$.puts({{json .Extra}})
case $.NSAlertSecondButtonReturn:$.exit(1)}
var fmt=$.NSDateFormatter.alloc.init
fmt.locale=$.NSLocale.localeWithLocaleIdentifier("en_US_POSIX");fmt.dateFormat={{json .Format}}
fmt.locale=$.NSLocale.localeWithLocaleIdentifier("en_US_POSIX")
fmt.dateFormat={{json .Format}}
fmt.stringFromDate(date.dateValue)
{{- end}}
{{define "dialog" -}}
@ -61,7 +66,9 @@ var opts={{json .Options}}
{{- if .IconPath}}
opts.withIcon=Path({{json .IconPath}})
{{- end}}
var res=app.{{.Operation}}({{json .Text}},opts)
try{var res=app.{{.Operation}}({{json .Text}},opts)}catch(e){if(e.errorNumber===-128)$.exit(1)
$.dprintf(2,e)
$.exit(-1)}
if(res.gaveUp){$.exit(5)}
if(res.buttonReturned==={{json .Extra}}){$.puts(res.buttonReturned)
$.exit(1)}
@ -71,13 +78,21 @@ res.textReturned
var app=Application.currentApplication()
app.includeStandardAdditions=true
app.activate()
var res=app.{{.Operation}}({{json .Options}})
ObjC.import('stdio')
ObjC.import('stdlib')
try{var res=app.{{.Operation}}({{json .Options}})}catch(e){if(e.errorNumber===-128)$.exit(1)
$.dprintf(2,e)
$.exit(-1)}
if(Array.isArray(res)){res.join({{json .Separator}})}else{res.toString()}
{{- end}}
{{define "list" -}}
var app=Application.currentApplication()
app.includeStandardAdditions=true
var res=app.chooseFromList({{json .Items}},{{json .Options}})
ObjC.import('stdio')
ObjC.import('stdlib')
try{var res=app.chooseFromList({{json .Items}},{{json .Options}})}catch(e){$.dprintf(2,e)
$.exit(-1)}
if(res===false)$.exit(1)
if(res.length!==0)res.join({{json .Separator}})
{{- end}}
{{define "notify" -}}
@ -97,12 +112,10 @@ Progress.totalUnitCount={{.Total}}
{{- if .Description}}
Progress.description={{json .Description}}
{{- end}}
while(true){var s
try{s=$.readline('')}catch(e){if(e.errorNumber===-128)$.exit(1)
while(true){try{var s=$.readline('')}catch(e){if(e.errorNumber===-128)$.exit(1)
break}
if(s.indexOf('#')===0){Progress.additionalDescription=s.slice(1)
continue}
var i=parseInt(s)
if(i>=0&&Progress.totalUnitCount>0){Progress.completedUnitCount=i
continue}}
if(i>=0&&Progress.totalUnitCount>0){Progress.completedUnitCount=i}}
{{- end}}`))

View file

@ -31,6 +31,9 @@ func main() {
if err != nil {
log.Fatal(err)
}
if bytes.ContainsAny(data, "`") {
log.Fatalf("illegal character: %s: `", name)
}
_, err = template.New(file.Name()).Funcs(funcs).Parse(string(data))
if err != nil {
log.Fatal(err)

View file

@ -2,5 +2,15 @@ var app = Application.currentApplication()
app.includeStandardAdditions = true
app.activate()
var res = app.chooseColor({defaultColor: {{json .}}})
ObjC.import('stdio')
ObjC.import('stdlib')
try {
var res = app.chooseColor({defaultColor: {{json .}}})
} catch (e) {
if (e.errorNumber === -128) $.exit(1)
$.dprintf(2, e)
$.exit(-1)
}
{'rgb(' + res.map(x => Math.round(x * 255)) + ')'}

View file

@ -20,21 +20,21 @@ alert.setMessageText({{json .Text}})
alert.addButtonWithTitle({{json .OK}})
alert.addButtonWithTitle({{json .Cancel}}).keyEquivalent = '\033'
{{- if .Info}}
alert.setInformativeText({{json .Info}})
alert.setInformativeText({{json .Info}})
{{- end}}
{{- if .Extra}}
alert.addButtonWithTitle({{json .Extra}})
alert.addButtonWithTitle({{json .Extra}})
{{- end}}
var res = alert.runModal
switch (res) {
case $.NSAlertThirdButtonReturn:
$.puts({{json .Extra}})
$.puts({{json .Extra}})
case $.NSAlertSecondButtonReturn:
$.exit(1)
$.exit(1)
}
var fmt = $.NSDateFormatter.alloc.init
fmt.locale = $.NSLocale.localeWithLocaleIdentifier("en_US_POSIX");
fmt.locale = $.NSLocale.localeWithLocaleIdentifier("en_US_POSIX")
fmt.dateFormat = {{json .Format}}
fmt.stringFromDate(date.dateValue)

View file

@ -7,15 +7,22 @@ ObjC.import('stdlib')
var opts = {{json .Options}}
{{- if .IconPath}}
opts.withIcon = Path({{json .IconPath}})
opts.withIcon = Path({{json .IconPath}})
{{- end}}
var res = app.{{.Operation}}({{json .Text}}, opts)
try {
var res = app.{{.Operation}}({{json .Text}}, opts)
} catch (e) {
if (e.errorNumber === -128) $.exit(1)
$.dprintf(2, e)
$.exit(-1)
}
if (res.gaveUp) {
$.exit(5)
$.exit(5)
}
if (res.buttonReturned === {{json .Extra}}) {
$.puts(res.buttonReturned)
$.exit(1)
$.puts(res.buttonReturned)
$.exit(1)
}
res.textReturned

View file

@ -2,9 +2,19 @@ var app = Application.currentApplication()
app.includeStandardAdditions = true
app.activate()
var res = app.{{.Operation}}({{json .Options}})
ObjC.import('stdio')
ObjC.import('stdlib')
try {
var res = app.{{.Operation}}({{json .Options}})
} catch (e) {
if (e.errorNumber === -128) $.exit(1)
$.dprintf(2, e)
$.exit(-1)
}
if (Array.isArray(res)) {
res.join({{json .Separator}})
res.join({{json .Separator}})
} else {
res.toString()
res.toString()
}

View file

@ -1,5 +1,15 @@
var app = Application.currentApplication()
app.includeStandardAdditions = true
var res = app.chooseFromList({{json .Items}}, {{json .Options}})
ObjC.import('stdio')
ObjC.import('stdlib')
try {
var res = app.chooseFromList({{json .Items}}, {{json .Options}})
} catch (e) {
$.dprintf(2, e)
$.exit(-1)
}
if (res === false) $.exit(1)
if (res.length !== 0) res.join({{json .Separator}})

View file

@ -13,9 +13,8 @@ ObjC.import('readline')
{{- end}}
while (true) {
var s
try {
s = $.readline('')
var s = $.readline('')
} catch (e) {
if (e.errorNumber === -128) $.exit(1)
break
@ -29,6 +28,5 @@ while (true) {
var i = parseInt(s)
if (i >= 0 && Progress.totalUnitCount > 0) {
Progress.completedUnitCount = i
continue
}
}

View file

@ -1,6 +1,8 @@
package zenity
import (
"os"
"github.com/ncruces/zenity/internal/zenutil"
)
@ -21,6 +23,10 @@ func message(kind messageKind, text string, opts options) error {
data.Operation = "displayDialog"
data.Options.Title = opts.title
if opts.customIcon != "" {
_, err := os.Stat(opts.customIcon)
if err != nil {
return err
}
data.IconPath = opts.customIcon
} else {
data.Options.Icon = opts.icon.String()