Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ziflex committed Nov 22, 2018
1 parent ee7a4a8 commit dba9761
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
20 changes: 18 additions & 2 deletions pkg/html/dynamic/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ const (
ScreenshotFormatJPEG ScreenshotFormat = "jpeg"
)

func handleLoadError(logger *zerolog.Logger, client *cdp.Client) {
err := client.Page.Close(context.Background())

if err != nil {
logger.Warn().Timestamp().Err(err).Msg("unabled to close document on load error")
}
}

func IsScreenshotFormatValid(format string) bool {
value := ScreenshotFormat(format)

Expand All @@ -62,6 +70,8 @@ func LoadHTMLDocument(
client *cdp.Client,
url string,
) (*HTMLDocument, error) {
logger := logging.FromContext(ctx)

if conn == nil {
return nil, core.Error(core.ErrMissedArgument, "connection")
}
Expand All @@ -76,24 +86,26 @@ func LoadHTMLDocument(
err = waitForLoadEvent(ctx, client)

if err != nil {
handleLoadError(logger, client)

return nil, err
}
}

node, err := getRootElement(ctx, client)

if err != nil {
handleLoadError(logger, client)
return nil, errors.Wrap(err, "failed to get root element")
}

broker, err := createEventBroker(client)

if err != nil {
handleLoadError(logger, client)
return nil, errors.Wrap(err, "failed to create event events")
}

logger := logging.FromContext(ctx)

rootElement, err := LoadElement(
ctx,
logger,
Expand All @@ -104,6 +116,10 @@ func LoadHTMLDocument(
)

if err != nil {
broker.Stop()
broker.Close()
handleLoadError(logger, client)

return nil, errors.Wrap(err, "failed to load root element")
}

Expand Down
4 changes: 4 additions & 0 deletions pkg/html/dynamic/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ func (drv *Driver) GetDocument(ctx context.Context, targetURL values.String) (va
},
)

if err != nil {
return nil, err
}

return LoadHTMLDocument(ctx, conn, client, url)
}

Expand Down

0 comments on commit dba9761

Please sign in to comment.