Skip to content

Commit

Permalink
Refactor errors (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
BrandonRomano authored Dec 21, 2016
1 parent d471be8 commit 546be77
Show file tree
Hide file tree
Showing 16 changed files with 209 additions and 422 deletions.
94 changes: 31 additions & 63 deletions controllers/boards_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,21 @@ func NewBoardsController(wc *wrecker.Wrecker) *BoardsController {
// Fetch loads a board from the board_spec (username/board-slug)
// Endpoint: [GET] /v1/boards/<board_spec:board>/
func (bc *BoardsController) Fetch(boardSpec string) (*models.Board, error) {
// Make request
response := new(models.Response)
response.Data = new(models.Board)
resp, err := bc.wreckerClient.Get("/boards/"+boardSpec).
// Build + execute request
resp := new(models.Response)
resp.Data = new(models.Board)
httpResp, err := bc.wreckerClient.Get("/boards/"+boardSpec).
URLParam("fields", models.BOARD_FIELDS).
Into(response).
Into(resp).
Execute()

// Error from Wrecker
if err != nil {
// Check Error
if err = models.WrapPinterestError(httpResp, resp, err); err != nil {
return nil, err
}

// Status code
if !(resp.StatusCode >= 200 && resp.StatusCode < 300) {
return nil, &models.PinterestError{
StatusCode: resp.StatusCode,
Message: response.Message,
}
}

// OK
return response.Data.(*models.Board), nil
return resp.Data.(*models.Board), nil
}

// BoardCreateOptionals is a struct that represents the optional parameters
Expand All @@ -57,31 +49,23 @@ type BoardCreateOptionals struct {
// Create makes a new board
// Endpoint: [POST] /v1/boards/
func (bc *BoardsController) Create(boardName string, optionals *BoardCreateOptionals) (*models.Board, error) {
// Make request
response := new(models.Response)
response.Data = new(models.Board)
resp, err := bc.wreckerClient.Post("/boards/").
// Build + execute request
resp := new(models.Response)
resp.Data = new(models.Board)
httpResp, err := bc.wreckerClient.Post("/boards/").
URLParam("fields", models.BOARD_FIELDS).
FormParam("name", boardName).
FormParam("description", optionals.Description).
Into(response).
Into(resp).
Execute()

// Error from Wrecker
if err != nil {
// Check Error
if err = models.WrapPinterestError(httpResp, resp, err); err != nil {
return nil, err
}

// Status code
if !(resp.StatusCode >= 200 && resp.StatusCode < 300) {
return nil, &models.PinterestError{
StatusCode: resp.StatusCode,
Message: response.Message,
}
}

// OK
return response.Data.(*models.Board), nil
return resp.Data.(*models.Board), nil
}

// BoardUpdateOptionals is a struct that represents the optional parameters
Expand All @@ -94,56 +78,40 @@ type BoardUpdateOptionals struct {
// Update updates an existing board
// Endpoint: [PATCH] /v1/boards/<board_spec:board>/
func (bc *BoardsController) Update(boardSpec string, optionals *BoardUpdateOptionals) (*models.Board, error) {
// Make request
response := new(models.Response)
response.Data = new(models.Board)
resp, err := bc.wreckerClient.Patch("/boards/"+boardSpec+"/").
// Build + execute request
resp := new(models.Response)
resp.Data = new(models.Board)
httpResp, err := bc.wreckerClient.Patch("/boards/"+boardSpec+"/").
URLParam("fields", models.BOARD_FIELDS).
FormParam("name", optionals.Name).
FormParam("description", optionals.Description).
Into(response).
Into(resp).
Execute()

// Error from Wrecker
if err != nil {
// Check Error
if err = models.WrapPinterestError(httpResp, resp, err); err != nil {
return nil, err
}

// Status code
if !(resp.StatusCode >= 200 && resp.StatusCode < 300) {
return nil, &models.PinterestError{
StatusCode: resp.StatusCode,
Message: response.Message,
}
}

// OK
return response.Data.(*models.Board), nil
return resp.Data.(*models.Board), nil
}

// Delete deletes an existing board
// Endpoint: [DELETE] /v1/boards/<board_spec:board>/
func (bc *BoardsController) Delete(boardSpec string) error {
// Make request
response := new(models.Response)
response.Data = ""
resp, err := bc.wreckerClient.Delete("/boards/" + boardSpec + "/").
Into(response).
// Build + execute request
resp := new(models.Response)
resp.Data = ""
httpResp, err := bc.wreckerClient.Delete("/boards/" + boardSpec + "/").
Into(resp).
Execute()

// Error from Wrecker
if err != nil {
// Check Error
if err = models.WrapPinterestError(httpResp, resp, err); err != nil {
return err
}

// Status code
if !(resp.StatusCode >= 200 && resp.StatusCode < 300) {
return &models.PinterestError{
StatusCode: resp.StatusCode,
Message: response.Message,
}
}

// OK
return nil
}
24 changes: 8 additions & 16 deletions controllers/boards_pins_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,19 @@ type BoardsPinsFetchOptionals struct {
// Fetch loads a board from the board_spec (username/board-slug)
// Endpoint: [GET] /v1/boards/<board_spec:board>/pins/
func (bpc *BoardsPinsController) Fetch(boardSpec string, optionals *BoardsPinsFetchOptionals) (*[]models.Pin, error) {
// Make request
response := new(models.Response)
response.Data = &[]models.Pin{}
resp, err := bpc.wreckerClient.Get("/boards/"+boardSpec+"/pins/").
// Build + execute request
resp := new(models.Response)
resp.Data = &[]models.Pin{}
httpResp, err := bpc.wreckerClient.Get("/boards/"+boardSpec+"/pins/").
URLParam("fields", models.PIN_FIELDS).
Into(response).
Into(resp).
Execute()

// Error from Wrecker
if err != nil {
// Check Error
if err = models.WrapPinterestError(httpResp, resp, err); err != nil {
return nil, err
}

// Status code
if !(resp.StatusCode >= 200 && resp.StatusCode < 300) {
return nil, &models.PinterestError{
StatusCode: resp.StatusCode,
Message: response.Message,
}
}

// OK
return response.Data.(*[]models.Pin), nil
return resp.Data.(*[]models.Pin), nil
}
22 changes: 7 additions & 15 deletions controllers/me_boards_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,18 @@ func newMeBoardsController(wc *wrecker.Wrecker) *MeBoardsController {
// Endpoint: [GET] /v1/me/boards/
func (mbc *MeBoardsController) Fetch() (*[]models.Board, error) {
// Build + execute request
response := new(models.Response)
response.Data = &[]models.Board{}
resp, err := mbc.wreckerClient.Get("/me/boards/").
resp := new(models.Response)
resp.Data = &[]models.Board{}
httpResp, err := mbc.wreckerClient.Get("/me/boards/").
URLParam("fields", models.BOARD_FIELDS).
Into(response).
Into(resp).
Execute()

// Error from Wrecker
if err != nil {
// Check Error
if err = models.WrapPinterestError(httpResp, resp, err); err != nil {
return nil, err
}

// Status code
if !(resp.StatusCode >= 200 && resp.StatusCode < 300) {
return nil, &models.PinterestError{
StatusCode: resp.StatusCode,
Message: response.Message,
}
}

// OK
return response.Data.(*[]models.Board), nil
return resp.Data.(*[]models.Board), nil
}
26 changes: 8 additions & 18 deletions controllers/me_boards_suggested_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,35 +29,25 @@ type MeBoardsSuggestedFetchOptionals struct {
// Fetch loads board suggestions for the logged in user
// Endpoint: [GET] /v1/me/boards/suggested/
func (mbsc *MeBoardsSuggestedController) Fetch(optionals *MeBoardsSuggestedFetchOptionals) (*[]models.Board, error) {
// Build request
response := new(models.Response)
response.Data = &[]models.Board{}
// Build + execute request
resp := new(models.Response)
resp.Data = &[]models.Board{}
request := mbsc.wreckerClient.Get("/me/boards/suggested/").
URLParam("fields", models.BOARD_FIELDS).
Into(response)
Into(resp)
if optionals.Count != 0 {
request.URLParam("count", strconv.Itoa(int(optionals.Count)))
}
if optionals.Pin != "" {
request.URLParam("pin", optionals.Pin)
}
httpResp, err := request.Execute()

// Execute request
resp, err := request.Execute()

// Error from Wrecker
if err != nil {
// Check Error
if err = models.WrapPinterestError(httpResp, resp, err); err != nil {
return nil, err
}

// Status code
if !(resp.StatusCode >= 200 && resp.StatusCode < 300) {
return nil, &models.PinterestError{
StatusCode: resp.StatusCode,
Message: response.Message,
}
}

// OK
return response.Data.(*[]models.Board), nil
return resp.Data.(*[]models.Board), nil
}
22 changes: 7 additions & 15 deletions controllers/me_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,18 @@ func NewMeController(wc *wrecker.Wrecker) *MeController {
// Endpoint: [GET] /v1/me/
func (mc *MeController) Fetch() (*models.User, error) {
// Build + execute request
response := new(models.Response)
response.Data = new(models.User)
resp, err := mc.wreckerClient.Get("/me/").
resp := new(models.Response)
resp.Data = new(models.User)
httpResp, err := mc.wreckerClient.Get("/me/").
URLParam("fields", models.USER_FIELDS).
Into(response).
Into(resp).
Execute()

// Error from Wrecker
if err != nil {
// Check Error
if err = models.WrapPinterestError(httpResp, resp, err); err != nil {
return nil, err
}

// Status code
if !(resp.StatusCode >= 200 && resp.StatusCode < 300) {
return nil, &models.PinterestError{
StatusCode: resp.StatusCode,
Message: response.Message,
}
}

// OK
return response.Data.(*models.User), nil
return resp.Data.(*models.User), nil
}
26 changes: 8 additions & 18 deletions controllers/me_followers_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,32 +27,22 @@ type MeFollowersFetchOptionals struct {
// Fetch loads the users that follow the logged in user
// Endpoint: [GET] /v1/me/boards/followers/
func (mfc *MeFollowersController) Fetch(optionals *MeFollowersFetchOptionals) (*[]models.User, *models.Page, error) {
// Build request
response := new(models.Response)
response.Data = &[]models.User{}
// Build + execute request
resp := new(models.Response)
resp.Data = &[]models.User{}
request := mfc.wreckerClient.Get("/me/followers/").
URLParam("fields", models.USER_FIELDS).
Into(response)
Into(resp)
if optionals.Cursor != "" {
request.URLParam("cursor", optionals.Cursor)
}
httpResp, err := request.Execute()

// Execute request
resp, err := request.Execute()

// Error from Wrecker
if err != nil {
// Check Error
if err = models.WrapPinterestError(httpResp, resp, err); err != nil {
return nil, nil, err
}

// Status code
if !(resp.StatusCode >= 200 && resp.StatusCode < 300) {
return nil, nil, &models.PinterestError{
StatusCode: resp.StatusCode,
Message: response.Message,
}
}

// OK
return response.Data.(*[]models.User), &response.Page, nil
return resp.Data.(*[]models.User), &resp.Page, nil
}
Loading

0 comments on commit 546be77

Please sign in to comment.