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
}

View File

@@ -83,7 +83,7 @@ func New(c echo.Context, logger *logger.Logger, config *config.Config, dirPath s
}
func formValues(c echo.Context, logger *logger.Logger) map[string]string {
const debugOp = "resource.formValues"
const op = "resource.formValues"
v := make(map[string]string)
v[ResultFilenameFormField] = c.FormValue(ResultFilenameFormField)
v[WaitTimeoutFormField] = c.FormValue(WaitTimeoutFormField)
@@ -97,15 +97,12 @@ func formValues(c echo.Context, logger *logger.Logger) map[string]string {
v[MarginLeftFormField] = c.FormValue(MarginLeftFormField)
v[MarginRightFormField] = c.FormValue(MarginRightFormField)
v[LandscapeFormField] = c.FormValue(LandscapeFormField)
logger.DebugfOp(debugOp, "%v", v)
logger.DebugfOp(op, "%v", v)
return v
}
func formFiles(c echo.Context, logger *logger.Logger, dirPath string) error {
const (
op = "formFiles"
debugOp = "resource.formFiles"
)
const op = "resource.formFiles"
form, err := c.MultipartForm()
if err != nil {
return &standarderror.Error{Op: op, Err: err}
@@ -132,7 +129,7 @@ func formFiles(c echo.Context, logger *logger.Logger, dirPath string) error {
if _, err := out.Seek(0, 0); err != nil {
return &standarderror.Error{Op: op, Err: err}
}
logger.DebugfOp(debugOp, "'%s' created", fh.Filename)
logger.DebugfOp(op, "'%s' created", fh.Filename)
}
}
return nil
@@ -293,7 +290,7 @@ func (r *Resource) Get(formField string) (string, error) {
}
func (r *Resource) value(formField string) (string, error) {
const op = "value"
const op = "resource.value"
v, ok := r.formValues[formField]
if !ok {
return "", &standarderror.Error{
@@ -306,7 +303,7 @@ func (r *Resource) value(formField string) (string, error) {
}
func (r *Resource) float64(formField string, defaultValue float64) (float64, error) {
const op = "float64"
const op = "resource.float64"
if !r.Has(formField) {
return defaultValue, nil
}
@@ -326,7 +323,7 @@ func (r *Resource) float64(formField string, defaultValue float64) (float64, err
}
func (r *Resource) bool(formField string, defaultValue bool) (bool, error) {
const op = "bool"
const op = "resource.bool"
if !r.Has(formField) {
return defaultValue, nil
}
@@ -366,7 +363,7 @@ func (r *Resource) Fpath(filename string) (string, error) {
}
func (r *Resource) content(filename string, defaultValue string) (string, error) {
const op = "content"
const op = "resource.content"
if !r.hasFile(filename) {
return defaultValue, nil
}
@@ -387,7 +384,7 @@ func (r *Resource) Fpaths(exts ...string) ([]string, error) {
const op = "resource.Fpaths"
var fpaths []string
err := filepath.Walk(r.formFilesDirPath, func(path string, info os.FileInfo, _ error) error {
const walkOp = "filepath.Walk"
const walkOp = "resource.filepath.Walk"
if info.IsDir() {
return nil
}