better op names + fixing issue with op chaining in errors

This commit is contained in:
Julien Neuhart
2019-07-08 10:28:43 +02:00
parent 5f6ab2fc08
commit 5af01505c2
11 changed files with 74 additions and 64 deletions

View File

@@ -35,10 +35,7 @@ const (
)
func convert(ctx *context.Context, p printer.Printer) error {
const (
op = "convert"
debugOp = "handler.convert"
)
const op = "handler.convert"
r := ctx.Resource()
logger := ctx.StandardLogger()
baseFilename := random.Get()
@@ -48,7 +45,7 @@ func convert(ctx *context.Context, p printer.Printer) error {
// and directly return the resulting PDF file
// or an error.
if !r.Has(resource.WebhookURLFormField) {
logger.DebugfOp(debugOp, "no '%s' found, converting synchronously", resource.WebhookURLFormField)
logger.DebugfOp(op, "no '%s' found, converting synchronously", resource.WebhookURLFormField)
if err := convertSync(filename, fpath, ctx, p); err != nil {
return &standarderror.Error{Op: op, Err: err}
}
@@ -57,15 +54,12 @@ func convert(ctx *context.Context, p printer.Printer) error {
// as a webhook URL has been given, we
// run the following lines in a goroutine so that
// it doesn't block.
logger.DebugfOp(debugOp, "'%s' found, converting asynchronously", resource.WebhookURLFormField)
logger.DebugfOp(op, "'%s' found, converting asynchronously", resource.WebhookURLFormField)
return convertAsync(filename, fpath, ctx, p)
}
func convertSync(filename, fpath string, ctx *context.Context, p printer.Printer) error {
const (
op = "convertSync"
debugOp = "handler.convertSync"
)
const op = "handler.convertSync"
r := ctx.Resource()
logger := ctx.StandardLogger()
if err := p.Print(fpath); err != nil {
@@ -73,7 +67,7 @@ func convertSync(filename, fpath string, ctx *context.Context, p printer.Printer
}
if !r.Has(resource.ResultFilenameFormField) {
logger.DebugfOp(
debugOp,
op,
"no '%s' found, using generated filename '%s'",
resource.ResultFilenameFormField,
filename,
@@ -84,7 +78,7 @@ func convertSync(filename, fpath string, ctx *context.Context, p printer.Printer
return nil
}
logger.DebugfOp(
debugOp,
op,
"'%s' found, so not using generated filename",
resource.ResultFilenameFormField,
)
@@ -99,10 +93,7 @@ func convertSync(filename, fpath string, ctx *context.Context, p printer.Printer
}
func convertAsync(filename, fpath string, ctx *context.Context, p printer.Printer) error {
const (
op = "convertAsync"
debugOp = "handler.convertAsync"
)
const op = "handler.convertAsync"
r := ctx.Resource()
logger := ctx.StandardLogger()
go func() {
@@ -132,7 +123,7 @@ func convertAsync(filename, fpath string, ctx *context.Context, p printer.Printe
return
}
logger.DebugfOp(
debugOp,
op,
"sending result file '%s' to '%s'",
filename,
webhookURL,

View File

@@ -4,21 +4,26 @@ import (
"github.com/labstack/echo/v4"
"github.com/thecodingmachine/gotenberg/internal/app/api/pkg/context"
"github.com/thecodingmachine/gotenberg/internal/pkg/printer"
"github.com/thecodingmachine/gotenberg/internal/pkg/standarderror"
)
// HTML is the endpoint for converting
// HTML to PDF.
func HTML(c echo.Context) error {
const op = "handler.HTML"
ctx := context.MustCastFromEchoContext(c)
r := ctx.Resource()
opts, err := r.ChromePrinterOptions()
if err != nil {
return err
return &standarderror.Error{Op: op, Err: err}
}
fpath, err := r.Fpath("index.html")
if err != nil {
return err
return &standarderror.Error{Op: op, Err: err}
}
p := printer.NewHTML(fpath, opts)
return convert(ctx, p)
if err := convert(ctx, p); err != nil {
return &standarderror.Error{Op: op, Err: err}
}
return nil
}

View File

@@ -4,24 +4,29 @@ import (
"github.com/labstack/echo/v4"
"github.com/thecodingmachine/gotenberg/internal/app/api/pkg/context"
"github.com/thecodingmachine/gotenberg/internal/pkg/printer"
"github.com/thecodingmachine/gotenberg/internal/pkg/standarderror"
)
// Markdown is the endpoint for converting
// Markdown to PDF.
func Markdown(c echo.Context) error {
const op = "handler.Markdown"
ctx := context.MustCastFromEchoContext(c)
r := ctx.Resource()
opts, err := r.ChromePrinterOptions()
if err != nil {
return err
return &standarderror.Error{Op: op, Err: err}
}
fpath, err := r.Fpath("index.html")
if err != nil {
return err
return &standarderror.Error{Op: op, Err: err}
}
p, err := printer.NewMarkdown(fpath, opts)
if err != nil {
return err
return &standarderror.Error{Op: op, Err: err}
}
return convert(ctx, p)
if err := convert(ctx, p); err != nil {
return &standarderror.Error{Op: op, Err: err}
}
return nil
}

View File

@@ -4,21 +4,26 @@ import (
"github.com/labstack/echo/v4"
"github.com/thecodingmachine/gotenberg/internal/app/api/pkg/context"
"github.com/thecodingmachine/gotenberg/internal/pkg/printer"
"github.com/thecodingmachine/gotenberg/internal/pkg/standarderror"
)
// Merge is the endpoint for
// merging PDF files.
func Merge(c echo.Context) error {
const op = "handler.Merge"
ctx := context.MustCastFromEchoContext(c)
r := ctx.Resource()
opts, err := r.MergePrinterOptions()
if err != nil {
return err
return &standarderror.Error{Op: op, Err: err}
}
fpaths, err := r.Fpaths(".pdf")
if err != nil {
return err
return &standarderror.Error{Op: op, Err: err}
}
p := printer.NewMerge(fpaths, opts)
return convert(ctx, p)
if err := convert(ctx, p); err != nil {
return &standarderror.Error{Op: op, Err: err}
}
return nil
}

View File

@@ -4,16 +4,18 @@ import (
"github.com/labstack/echo/v4"
"github.com/thecodingmachine/gotenberg/internal/app/api/pkg/context"
"github.com/thecodingmachine/gotenberg/internal/pkg/printer"
"github.com/thecodingmachine/gotenberg/internal/pkg/standarderror"
)
// Office is the endpoint for converting
// Office files to PDF.
func Office(c echo.Context) error {
const op = "handler.Office"
ctx := context.MustCastFromEchoContext(c)
r := ctx.Resource()
opts, err := r.OfficePrinterOptions()
if err != nil {
return err
return &standarderror.Error{Op: op, Err: err}
}
fpaths, err := r.Fpaths(
".txt",
@@ -30,8 +32,11 @@ func Office(c echo.Context) error {
".odp",
)
if err != nil {
return err
return &standarderror.Error{Op: op, Err: err}
}
p := printer.NewOffice(fpaths, opts)
return convert(ctx, p)
if err := convert(ctx, p); err != nil {
return &standarderror.Error{Op: op, Err: err}
}
return nil
}

View File

@@ -5,21 +5,26 @@ import (
"github.com/thecodingmachine/gotenberg/internal/app/api/pkg/context"
"github.com/thecodingmachine/gotenberg/internal/app/api/pkg/resource"
"github.com/thecodingmachine/gotenberg/internal/pkg/printer"
"github.com/thecodingmachine/gotenberg/internal/pkg/standarderror"
)
// URL is the endpoint for converting
// a URL to PDF.
func URL(c echo.Context) error {
const op = "handler.URL"
ctx := context.MustCastFromEchoContext(c)
r := ctx.Resource()
opts, err := r.ChromePrinterOptions()
if err != nil {
return err
return &standarderror.Error{Op: op, Err: err}
}
remoteURL, err := r.Get(resource.RemoteURLFormField)
if err != nil {
return err
return &standarderror.Error{Op: op, Err: err}
}
p := printer.NewURL(remoteURL, opts)
return convert(ctx, p)
if err := convert(ctx, p); err != nil {
return &standarderror.Error{Op: op, Err: err}
}
return nil
}