Giter VIP home page Giter VIP logo

Comments (7)

balacode avatar balacode commented on July 18, 2024

Thank you for valuable input! I'm changing SaveFile() to return error instead of *PDF.

IO can have errors beyond the control of the library and because SaveFile() is normally the last method called on PDF, it does not need to return *PDF for method chaining.

Most other methods log logic errors (e.g. "unknown page size") and return *PDF. To change them to return error would mean to remove method chaining. I'm thinking of another means to query for errors in these cases: perhaps a pdf.Err() method that is set to an instance of the last error.

You can also redefine PDFErrorHandler to another function with the same signature as fmt.Println:
func(a ...interface{}) (n int, err error). (However, soon the error handler will be set per PDF instance, not globally.)

Your additional suggestions are welcome.

from one-file-pdf.

tadvi avatar tadvi commented on July 18, 2024

Do you have to have method chaining?

pdf.Err() would probably be ok, as long as errors are returned instead of logging.

from one-file-pdf.

balacode avatar balacode commented on July 18, 2024

Chaining is a good feature to have:

pdf.SetXY(5, 5).SetColor("red").DrawText("hello")

without chaining, will be:

pdf.SetXY(5,5)
pdf.SetColor("red")
pdf.DrawText("hello")

... and you can't put all these statements on one line because go fmt would split them into many lines.
I thought of returning (*PDF, error) but that won't work:

multiple-value pdf.SetX() in single-value context

Please excuse my long explanation, its so that anyone who looks at this issue considers it,

from one-file-pdf.

tadvi avatar tadvi commented on July 18, 2024

If you must have chaining then store first error value inside pdf.Err. That way it would be reachable after chain of calls. Thanks.

from one-file-pdf.

balacode avatar balacode commented on July 18, 2024

I've changed SaveFile() to return error now, and some other changes (see changelog). Yet to add Err(), but soon. Again, thank you for pointing it out.

from one-file-pdf.

balacode avatar balacode commented on July 18, 2024

Hi, I've created some error handling methods in commit fd06c6f:

Clean() *PDF - clears all accumulated errors.
Errors() []error - returns a slice of all accumulated errors
PullError() error - removes and returns the first error from the errors collection

In the past few days I've made some changes to remove unnecessary error conditions that should never occur in the first place. For example, the checks for a page being active, since now there is always an active page.

Other changes I'm planning soon:

  • To add the name of the method to each error message, so you can know
    where in the chain the error occurred.
  • To remove some other spurious error checks: e.g. the "Rune out of range" error in textWidthPt1000() that should never happen in the first place, because we're not using Unicode yet, and even then it won't be needed.

  • There may be minor changes/fixes to error handling over the weekend.

from one-file-pdf.

balacode avatar balacode commented on July 18, 2024
  • Error messages now include the name of the method that produced the error
  • Clean(), Errors() and PullError() have unit tests, work as expected

from one-file-pdf.

Related Issues (11)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.