errors.Is -> xerrors.Is and %w -> %v for go 1.12 compatibility

This commit is contained in:
Peter Williams 2020-06-29 20:53:58 +10:00
parent 9caa40e212
commit b7f91fd72c
7 changed files with 15 additions and 11 deletions

View File

@ -47,7 +47,7 @@ func New(page *model.PdfPage) (*Extractor, error) {
mediaBox, err := page.GetMediaBox()
if err != nil {
return nil, fmt.Errorf("extractor requires mediaBox. %w", err)
return nil, fmt.Errorf("extractor requires mediaBox. %v", err)
}
e := &Extractor{
contents: contents,

View File

@ -20,6 +20,7 @@ import (
"github.com/unidoc/unipdf/v3/internal/textencoding"
"github.com/unidoc/unipdf/v3/internal/transform"
"github.com/unidoc/unipdf/v3/model"
"golang.org/x/xerrors"
)
// maxFormStack is the maximum form stack recursion depth. It has to be low enough to avoid a stack
@ -74,7 +75,7 @@ func (e *Extractor) extractPageText(contents string, resources *model.PdfPageRes
if level > maxFormStack {
err := errors.New("form stack overflow")
common.Log.Debug("ERROR: extractPageText. recursion level=%d err=%w", level, err)
common.Log.Debug("ERROR: extractPageText. recursion level=%d err=%v", level, err)
return pageText, state.numChars, state.numMisses, err
}
@ -86,7 +87,7 @@ func (e *Extractor) extractPageText(contents string, resources *model.PdfPageRes
cstreamParser := contentstream.NewContentStreamParser(contents)
operations, err := cstreamParser.Parse()
if err != nil {
common.Log.Debug("ERROR: extractPageText parse failed. err=%w", err)
common.Log.Debug("ERROR: extractPageText parse failed. err=%v", err)
return pageText, state.numChars, state.numMisses, err
}
@ -245,7 +246,7 @@ func (e *Extractor) extractPageText(contents string, resources *model.PdfPageRes
return err
}
err = to.setFont(name, size)
to.invalidFont = errors.Is(err, core.ErrNotSupported)
to.invalidFont = xerrors.Is(err, core.ErrNotSupported)
if err != nil && !to.invalidFont {
return err
}

1
go.mod
View File

@ -15,4 +15,5 @@ require (
golang.org/x/image v0.0.0-20181116024801-cd38e8056d9b
golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1 // indirect
golang.org/x/text v0.3.2
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543
)

2
go.sum
View File

@ -56,6 +56,8 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

View File

@ -55,7 +55,7 @@ func NewSimpleTextEncoder(baseName string, differences map[CharCode]GlyphName) (
fnc, ok := simple[baseName]
if !ok {
common.Log.Debug("ERROR: NewSimpleTextEncoder. Unknown encoding %q", baseName)
return nil, fmt.Errorf("unsupported font encoding: %q (%w)", baseName, core.ErrNotSupported)
return nil, fmt.Errorf("unsupported font encoding: %q (%v)", baseName, core.ErrNotSupported)
}
enc := fnc()
if len(differences) != 0 {

View File

@ -21,8 +21,8 @@ var (
errRangeError = errors.New("range check error")
ErrEncrypted = errors.New("file needs to be decrypted first")
ErrNoFont = errors.New("font not defined")
ErrFontNotSupported = fmt.Errorf("unsupported font (%w)", core.ErrNotSupported)
ErrType1CFontNotSupported = fmt.Errorf("Type1C fonts are not currently supported (%w)", core.ErrNotSupported)
ErrType3FontNotSupported = fmt.Errorf("Type3 fonts are not currently supported (%w)", core.ErrNotSupported)
ErrTTCmapNotSupported = fmt.Errorf("unsupported TrueType cmap format (%w)", core.ErrNotSupported)
ErrFontNotSupported = fmt.Errorf("unsupported font (%v)", core.ErrNotSupported)
ErrType1CFontNotSupported = fmt.Errorf("Type1C fonts are not currently supported (%v)", core.ErrNotSupported)
ErrType3FontNotSupported = fmt.Errorf("Type3 fonts are not currently supported (%v)", core.ErrNotSupported)
ErrTTCmapNotSupported = fmt.Errorf("unsupported TrueType cmap format (%v)", core.ErrNotSupported)
)

View File

@ -209,7 +209,7 @@ func (t *ttfParser) Parse() (TtfType, error) {
}
if version == "OTTO" {
// See https://docs.microsoft.com/en-us/typography/opentype/spec/otff
return TtfType{}, fmt.Errorf("fonts based on PostScript outlines are not supported (%w)",
return TtfType{}, fmt.Errorf("fonts based on PostScript outlines are not supported (%v)",
core.ErrNotSupported)
}
if version != "\x00\x01\x00\x00" && version != "true" {
@ -377,7 +377,7 @@ func (t *ttfParser) parseCmapSubtable31(offset31 int64) error {
t.f.Seek(int64(t.tables["cmap"])+offset31, os.SEEK_SET)
format := t.ReadUShort()
if format != 4 {
return fmt.Errorf("unexpected subtable format: %d (%w)", format, core.ErrNotSupported)
return fmt.Errorf("unexpected subtable format: %d (%v)", format, core.ErrNotSupported)
}
t.Skip(2 * 2) // length, language
segCount := int(t.ReadUShort() / 2)