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 ico = zenity.WarningIcon
case "dialog-password": case "dialog-password":
ico = zenity.PasswordIcon ico = zenity.PasswordIcon
default: case unspecified:
ico = zenity.NoIcon ico = zenity.NoIcon
default:
opts = append(opts, zenity.CustomIcon(icon))
} }
if icon != unspecified { if ico != 0 {
opts = append(opts, ico) opts = append(opts, ico)
} }

View file

@ -1,6 +1,8 @@
package zenity package zenity
import ( import (
"os"
"github.com/ncruces/zenity/internal/zenutil" "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.Hidden = opts.hideText
data.Options.Timeout = zenutil.Timeout data.Options.Timeout = zenutil.Timeout
if opts.customIcon != "" { if opts.customIcon != "" {
_, err := os.Stat(opts.customIcon)
if err != nil {
return "", err
}
data.IconPath = opts.customIcon data.IconPath = opts.customIcon
} else { } else {
data.Options.Icon = opts.icon.String() 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() var app=Application.currentApplication()
app.includeStandardAdditions=true app.includeStandardAdditions=true
app.activate() 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))+')'} {'rgb('+res.map(x=>Math.round(x*255))+')'}
{{- end}} {{- end}}
{{define "date" -}} {{define "date" -}}
@ -48,7 +52,8 @@ var res=alert.runModal
switch(res){case $.NSAlertThirdButtonReturn:$.puts({{json .Extra}}) switch(res){case $.NSAlertThirdButtonReturn:$.puts({{json .Extra}})
case $.NSAlertSecondButtonReturn:$.exit(1)} case $.NSAlertSecondButtonReturn:$.exit(1)}
var fmt=$.NSDateFormatter.alloc.init 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) fmt.stringFromDate(date.dateValue)
{{- end}} {{- end}}
{{define "dialog" -}} {{define "dialog" -}}
@ -61,7 +66,9 @@ var opts={{json .Options}}
{{- if .IconPath}} {{- if .IconPath}}
opts.withIcon=Path({{json .IconPath}}) opts.withIcon=Path({{json .IconPath}})
{{- end}} {{- 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.gaveUp){$.exit(5)}
if(res.buttonReturned==={{json .Extra}}){$.puts(res.buttonReturned) if(res.buttonReturned==={{json .Extra}}){$.puts(res.buttonReturned)
$.exit(1)} $.exit(1)}
@ -71,13 +78,21 @@ res.textReturned
var app=Application.currentApplication() var app=Application.currentApplication()
app.includeStandardAdditions=true app.includeStandardAdditions=true
app.activate() 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()} if(Array.isArray(res)){res.join({{json .Separator}})}else{res.toString()}
{{- end}} {{- end}}
{{define "list" -}} {{define "list" -}}
var app=Application.currentApplication() var app=Application.currentApplication()
app.includeStandardAdditions=true 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}}) if(res.length!==0)res.join({{json .Separator}})
{{- end}} {{- end}}
{{define "notify" -}} {{define "notify" -}}
@ -97,12 +112,10 @@ Progress.totalUnitCount={{.Total}}
{{- if .Description}} {{- if .Description}}
Progress.description={{json .Description}} Progress.description={{json .Description}}
{{- end}} {{- end}}
while(true){var s while(true){try{var s=$.readline('')}catch(e){if(e.errorNumber===-128)$.exit(1)
try{s=$.readline('')}catch(e){if(e.errorNumber===-128)$.exit(1)
break} break}
if(s.indexOf('#')===0){Progress.additionalDescription=s.slice(1) if(s.indexOf('#')===0){Progress.additionalDescription=s.slice(1)
continue} continue}
var i=parseInt(s) var i=parseInt(s)
if(i>=0&&Progress.totalUnitCount>0){Progress.completedUnitCount=i if(i>=0&&Progress.totalUnitCount>0){Progress.completedUnitCount=i}}
continue}}
{{- end}}`)) {{- end}}`))

View file

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

View file

@ -2,5 +2,15 @@ var app = Application.currentApplication()
app.includeStandardAdditions = true app.includeStandardAdditions = true
app.activate() 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)) + ')'} {'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 .OK}})
alert.addButtonWithTitle({{json .Cancel}}).keyEquivalent = '\033' alert.addButtonWithTitle({{json .Cancel}}).keyEquivalent = '\033'
{{- if .Info}} {{- if .Info}}
alert.setInformativeText({{json .Info}}) alert.setInformativeText({{json .Info}})
{{- end}} {{- end}}
{{- if .Extra}} {{- if .Extra}}
alert.addButtonWithTitle({{json .Extra}}) alert.addButtonWithTitle({{json .Extra}})
{{- end}} {{- end}}
var res = alert.runModal var res = alert.runModal
switch (res) { switch (res) {
case $.NSAlertThirdButtonReturn: case $.NSAlertThirdButtonReturn:
$.puts({{json .Extra}}) $.puts({{json .Extra}})
case $.NSAlertSecondButtonReturn: case $.NSAlertSecondButtonReturn:
$.exit(1) $.exit(1)
} }
var fmt = $.NSDateFormatter.alloc.init var fmt = $.NSDateFormatter.alloc.init
fmt.locale = $.NSLocale.localeWithLocaleIdentifier("en_US_POSIX"); fmt.locale = $.NSLocale.localeWithLocaleIdentifier("en_US_POSIX")
fmt.dateFormat = {{json .Format}} fmt.dateFormat = {{json .Format}}
fmt.stringFromDate(date.dateValue) fmt.stringFromDate(date.dateValue)

View file

@ -7,15 +7,22 @@ ObjC.import('stdlib')
var opts = {{json .Options}} var opts = {{json .Options}}
{{- if .IconPath}} {{- if .IconPath}}
opts.withIcon = Path({{json .IconPath}}) opts.withIcon = Path({{json .IconPath}})
{{- end}} {{- 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) { if (res.gaveUp) {
$.exit(5) $.exit(5)
} }
if (res.buttonReturned === {{json .Extra}}) { if (res.buttonReturned === {{json .Extra}}) {
$.puts(res.buttonReturned) $.puts(res.buttonReturned)
$.exit(1) $.exit(1)
} }
res.textReturned res.textReturned

View file

@ -2,9 +2,19 @@ var app = Application.currentApplication()
app.includeStandardAdditions = true app.includeStandardAdditions = true
app.activate() 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)) { if (Array.isArray(res)) {
res.join({{json .Separator}}) res.join({{json .Separator}})
} else { } else {
res.toString() res.toString()
} }

View file

@ -1,5 +1,15 @@
var app = Application.currentApplication() var app = Application.currentApplication()
app.includeStandardAdditions = true 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}}) if (res.length !== 0) res.join({{json .Separator}})

View file

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

View file

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