Skip to content

Commit

Permalink
Better error handling for using external images
Browse files Browse the repository at this point in the history
This fixes #241
  • Loading branch information
pgundlach committed Nov 19, 2019
1 parent 349579e commit 5b3895c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
8 changes: 6 additions & 2 deletions src/go/splib/splib.go
Expand Up @@ -16,6 +16,10 @@ import (
"splibaux"
)

var (
errorpattern = `**err`
)

// Convert a string slice to a C char* array and add a NULL pointer.
func toCharArray(s []string) **C.char {
cArray := C.malloc(C.size_t(len(s)+1) * C.size_t(unsafe.Sizeof(uintptr(0))))
Expand Down Expand Up @@ -138,7 +142,7 @@ func addDir(p string) {
func lookupFile(path string) *C.char {
ret, err := splibaux.GetFullPath(path)
if err != nil {
fmt.Println(err)
return s2c(errorpattern + err.Error())
}
return s2c(ret)
}
Expand All @@ -153,7 +157,7 @@ func listFonts() **C.char {
func convertSVGImage(path string) *C.char {
ret, err := splibaux.ConvertSVGImage(path)
if err != nil {
fmt.Println(err)
return s2c(errorpattern + err.Error())
}
return s2c(ret)
}
Expand Down
5 changes: 4 additions & 1 deletion src/go/splibaux/splibaux.go
Expand Up @@ -49,7 +49,10 @@ func saveFileFromUrl(parsedURL *url.URL, rawURL string) (string, error) {
return "", fmt.Errorf("Image cache %q exists but is not a directory", rawimgcache)
}
} else {
os.MkdirAll(rawimgcache, 0755)
err = os.MkdirAll(rawimgcache, 0755)
if err != nil {
return "", err
}
}

destfile := parsedURL.Hostname() + parsedURL.Path
Expand Down
15 changes: 13 additions & 2 deletions src/lua/splib.lua
Expand Up @@ -53,6 +53,7 @@ extern char* convertSVGImage(GoString p0);
]]

ld = ffi.load("libsplib")
errorpattern = "^%*%*err"

local function c(str)
return ffi.new("GoString",str,#str)
Expand Down Expand Up @@ -104,9 +105,13 @@ end
local function lookupfile(filename)
local ret = ld.lookupFile(c(filename))
local _ret = ffi.string(ret)
if string.match( _ret,errorpattern ) then
err(string.gsub( _ret,errorpattern ,"" ))
return
end
if _ret == "" then return nil end

return ffi.string(ret)
return _ret
end

local function listfonts()
Expand All @@ -123,9 +128,15 @@ end
local function convertSVGImage(filename)
local ret = ld.convertSVGImage(c(filename))
local _ret = ffi.string(ret)

if string.match( _ret,errorpattern ) then
err(string.gsub( _ret,errorpattern ,"" ))
return
end

if _ret == "" then return nil end

return ffi.string(ret)
return _ret
end


Expand Down

0 comments on commit 5b3895c

Please sign in to comment.