Skip to content

Commit

Permalink
Move SetGeolocation validation into business logic
Browse files Browse the repository at this point in the history
Separating the transformation of a Sobek value from the validation of
it.
  • Loading branch information
inancgumus committed Nov 5, 2024
1 parent a1619b9 commit e384842
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
10 changes: 10 additions & 0 deletions common/browser_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,16 @@ func (b *BrowserContext) SetDefaultTimeout(timeout int64) {
func (b *BrowserContext) SetGeolocation(g *Geolocation) error {
b.logger.Debugf("BrowserContext:SetGeolocation", "bctxid:%v", b.id)

if g.Longitude < -180 || g.Longitude > 180 {
return fmt.Errorf(`invalid longitude "%.2f": precondition -180 <= LONGITUDE <= 180 failed`, g.Longitude)
}
if g.Latitude < -90 || g.Latitude > 90 {
return fmt.Errorf(`invalid latitude "%.2f": precondition -90 <= LATITUDE <= 90 failed`, g.Latitude)
}
if g.Accuracy < 0 {
return fmt.Errorf(`invalid accuracy "%.2f": precondition 0 <= ACCURACY failed`, g.Accuracy)
}

b.opts.Geolocation = g
for _, p := range b.browser.getPages() {
if err := p.updateGeolocation(); err != nil {
Expand Down
10 changes: 0 additions & 10 deletions common/browser_context_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,6 @@ func (g *Geolocation) Parse(ctx context.Context, sopts sobek.Value) error { //no
}
}

if newgl.Longitude < -180 || newgl.Longitude > 180 {
return fmt.Errorf(`invalid longitude "%.2f": precondition -180 <= LONGITUDE <= 180 failed`, newgl.Longitude)
}
if newgl.Latitude < -90 || newgl.Latitude > 90 {
return fmt.Errorf(`invalid latitude "%.2f": precondition -90 <= LATITUDE <= 90 failed`, newgl.Latitude)
}
if newgl.Accuracy < 0 {
return fmt.Errorf(`invalid accuracy "%.2f": precondition 0 <= ACCURACY failed`, newgl.Accuracy)
}

*g = newgl

return nil
Expand Down

0 comments on commit e384842

Please sign in to comment.