Skip to content

Commit

Permalink
Merge pull request #1555 from felder-cl/fix/controllers-use-cause
Browse files Browse the repository at this point in the history
controllers: Fix checking errors via ErrorNotFound
  • Loading branch information
felder-cl committed Aug 20, 2019
2 parents 1b2bb1c + b2de79e commit 4b8b8fc
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 15 deletions.
8 changes: 4 additions & 4 deletions core/web/bridge_types_controller.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package web

import (
"errors"
"fmt"
"net/http"

"github.com/gin-gonic/gin"
"github.com/pkg/errors"
"github.com/smartcontractkit/chainlink/core/services"
"github.com/smartcontractkit/chainlink/core/store/models"
"github.com/smartcontractkit/chainlink/core/store/orm"
Expand Down Expand Up @@ -44,7 +44,7 @@ func (btc *BridgeTypesController) Show(c *gin.Context) {
name := c.Param("BridgeName")
if taskType, err := models.NewTaskType(name); err != nil {
jsonAPIError(c, http.StatusUnprocessableEntity, err)
} else if bt, err := btc.App.GetStore().FindBridge(taskType); err == orm.ErrorNotFound {
} else if bt, err := btc.App.GetStore().FindBridge(taskType); errors.Cause(err) == orm.ErrorNotFound {
jsonAPIError(c, http.StatusNotFound, errors.New("bridge not found"))
} else if err != nil {
jsonAPIError(c, http.StatusInternalServerError, err)
Expand All @@ -60,7 +60,7 @@ func (btc *BridgeTypesController) Update(c *gin.Context) {

if taskType, err := models.NewTaskType(name); err != nil {
jsonAPIError(c, http.StatusUnprocessableEntity, err)
} else if bt, err := btc.App.GetStore().FindBridge(taskType); err == orm.ErrorNotFound {
} else if bt, err := btc.App.GetStore().FindBridge(taskType); errors.Cause(err) == orm.ErrorNotFound {
jsonAPIError(c, http.StatusNotFound, errors.New("bridge not found"))
} else if err != nil {
jsonAPIError(c, http.StatusInternalServerError, err)
Expand All @@ -79,7 +79,7 @@ func (btc *BridgeTypesController) Destroy(c *gin.Context) {

if taskType, err := models.NewTaskType(name); err != nil {
jsonAPIError(c, http.StatusUnprocessableEntity, err)
} else if bt, err := btc.App.GetStore().FindBridge(taskType); err == orm.ErrorNotFound {
} else if bt, err := btc.App.GetStore().FindBridge(taskType); errors.Cause(err) == orm.ErrorNotFound {
jsonAPIError(c, http.StatusNotFound, errors.New("bridge not found"))
} else if err != nil {
jsonAPIError(c, http.StatusInternalServerError, fmt.Errorf("Error searching for bridge for BTC Destroy: %+v", err))
Expand Down
3 changes: 2 additions & 1 deletion core/web/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/gin-gonic/gin"
"github.com/manyminds/api2go/jsonapi"
"github.com/pkg/errors"
"github.com/smartcontractkit/chainlink/core/store/models"
"github.com/smartcontractkit/chainlink/core/store/orm"
)
Expand Down Expand Up @@ -41,7 +42,7 @@ func paginatedResponse(
count int,
err error,
) {
if err == orm.ErrorNotFound {
if errors.Cause(err) == orm.ErrorNotFound {
err = nil
}

Expand Down
8 changes: 4 additions & 4 deletions core/web/job_runs_controller.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package web

import (
"errors"
"io/ioutil"
"net/http"

"github.com/gin-gonic/gin"
"github.com/pkg/errors"
"github.com/smartcontractkit/chainlink/core/services"
"github.com/smartcontractkit/chainlink/core/store/models"
"github.com/smartcontractkit/chainlink/core/store/orm"
Expand Down Expand Up @@ -48,7 +48,7 @@ func (jrc *JobRunsController) Index(c *gin.Context, size, page, offset int) {
func (jrc *JobRunsController) Create(c *gin.Context) {
id := c.Param("SpecID")

if j, err := jrc.App.GetStore().FindJob(id); err == orm.ErrorNotFound {
if j, err := jrc.App.GetStore().FindJob(id); errors.Cause(err) == orm.ErrorNotFound {
jsonAPIError(c, http.StatusNotFound, errors.New("Job not found"))
} else if err != nil {
jsonAPIError(c, http.StatusInternalServerError, err)
Expand Down Expand Up @@ -76,7 +76,7 @@ func getRunData(c *gin.Context) (models.JSON, error) {
// "<application>/runs/:RunID"
func (jrc *JobRunsController) Show(c *gin.Context) {
id := c.Param("RunID")
if jr, err := jrc.App.GetStore().FindJobRun(id); err == orm.ErrorNotFound {
if jr, err := jrc.App.GetStore().FindJobRun(id); errors.Cause(err) == orm.ErrorNotFound {
jsonAPIError(c, http.StatusNotFound, errors.New("Job run not found"))
} else if err != nil {
jsonAPIError(c, http.StatusInternalServerError, err)
Expand All @@ -96,7 +96,7 @@ func (jrc *JobRunsController) Update(c *gin.Context) {
var brr models.BridgeRunResult

unscoped := jrc.App.GetStore().Unscoped()
if jr, err := unscoped.FindJobRun(id); err == orm.ErrorNotFound {
if jr, err := unscoped.FindJobRun(id); errors.Cause(err) == orm.ErrorNotFound {
jsonAPIError(c, http.StatusNotFound, errors.New("Job Run not found"))
} else if err != nil {
jsonAPIError(c, http.StatusInternalServerError, err)
Expand Down
6 changes: 3 additions & 3 deletions core/web/job_specs_controller.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package web

import (
"errors"
"net/http"

"github.com/gin-gonic/gin"
"github.com/pkg/errors"
"github.com/smartcontractkit/chainlink/core/services"
"github.com/smartcontractkit/chainlink/core/store/models"
"github.com/smartcontractkit/chainlink/core/store/orm"
Expand Down Expand Up @@ -58,7 +58,7 @@ func (jsc *JobSpecsController) Create(c *gin.Context) {
// "<application>/specs/:SpecID"
func (jsc *JobSpecsController) Show(c *gin.Context) {
id := c.Param("SpecID")
if j, err := jsc.App.GetStore().FindJob(id); err == orm.ErrorNotFound {
if j, err := jsc.App.GetStore().FindJob(id); errors.Cause(err) == orm.ErrorNotFound {
jsonAPIError(c, http.StatusNotFound, errors.New("JobSpec not found"))
} else if err != nil {
jsonAPIError(c, http.StatusInternalServerError, err)
Expand All @@ -72,7 +72,7 @@ func (jsc *JobSpecsController) Show(c *gin.Context) {
// "<application>/specs/:SpecID"
func (jsc *JobSpecsController) Destroy(c *gin.Context) {
id := c.Param("SpecID")
if err := jsc.App.ArchiveJob(id); err == orm.ErrorNotFound {
if err := jsc.App.ArchiveJob(id); errors.Cause(err) == orm.ErrorNotFound {
jsonAPIError(c, http.StatusNotFound, errors.New("JobSpec not found"))
} else if err != nil {
jsonAPIError(c, http.StatusInternalServerError, err)
Expand Down
6 changes: 3 additions & 3 deletions core/web/service_agreements_controller.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package web

import (
"errors"
"net/http"

"github.com/ethereum/go-ethereum/common"
"github.com/gin-gonic/gin"
"github.com/pkg/errors"
"github.com/smartcontractkit/chainlink/core/services"
"github.com/smartcontractkit/chainlink/core/store/models"
"github.com/smartcontractkit/chainlink/core/store/orm"
Expand All @@ -31,7 +31,7 @@ func (sac *ServiceAgreementsController) Create(c *gin.Context) {
}

sa, err := sac.App.GetStore().FindServiceAgreement(us.ID.String())
if err == orm.ErrorNotFound {
if errors.Cause(err) == orm.ErrorNotFound {
sa, err = models.BuildServiceAgreement(us, sac.App.GetStore().KeyStore)
if err != nil {
jsonAPIError(c, http.StatusUnprocessableEntity, err)
Expand All @@ -52,7 +52,7 @@ func (sac *ServiceAgreementsController) Create(c *gin.Context) {
// "<application>/service_agreements/:SAID"
func (sac *ServiceAgreementsController) Show(c *gin.Context) {
id := common.HexToHash(c.Param("SAID"))
if sa, err := sac.App.GetStore().FindServiceAgreement(id.String()); err == orm.ErrorNotFound {
if sa, err := sac.App.GetStore().FindServiceAgreement(id.String()); errors.Cause(err) == orm.ErrorNotFound {
jsonAPIError(c, http.StatusNotFound, errors.New("ServiceAgreement not found"))
} else if err != nil {
jsonAPIError(c, http.StatusInternalServerError, err)
Expand Down

0 comments on commit 4b8b8fc

Please sign in to comment.