Skip to content

Commit

Permalink
add http errors to handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
FACorreiaa committed Jun 22, 2024
1 parent c317a1a commit bd2f813
Show file tree
Hide file tree
Showing 12 changed files with 157 additions and 73 deletions.
30 changes: 30 additions & 0 deletions app/errors/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package httperror

import (
"encoding/json"
"net/http"
)

type HTTPError struct {
Message string `json:"message"`
StatusCode int `json:"status_code"`
}

func (e *HTTPError) WriteError(w http.ResponseWriter) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(e.StatusCode)
json.NewEncoder(w).Encode(e)
}

var (
ErrInvalidID = &HTTPError{Message: "Invalid ID", StatusCode: http.StatusBadRequest}
ErrNotFound = &HTTPError{Message: "Resource not found", StatusCode: http.StatusNotFound}
ErrInternalServer = &HTTPError{Message: "Internal server error", StatusCode: http.StatusInternalServerError}
)

func NewHTTPError(message string, statusCode int) *HTTPError {
return &HTTPError{
Message: message,
StatusCode: statusCode,
}
}
29 changes: 23 additions & 6 deletions app/handlers/airline.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"context"

httperror "github.com/FACorreiaa/Aviation-tracker/app/errors"
"github.com/FACorreiaa/Aviation-tracker/app/models"
svg2 "github.com/FACorreiaa/Aviation-tracker/app/static/svg"
airline "github.com/FACorreiaa/Aviation-tracker/app/view/airlines"
Expand All @@ -30,7 +31,7 @@ func (h *Handler) renderAirlineSidebar() []models.SidebarItem {
return sidebar
}

func (h *Handler) getAirline(_ http.ResponseWriter, r *http.Request) (int, []models.Airline, error) {
func (h *Handler) getAirline(w http.ResponseWriter, r *http.Request) (int, []models.Airline, error) {
pageSize := 20
name := r.FormValue("airline_name")
callSign := r.FormValue("call_sign")
Expand All @@ -56,18 +57,20 @@ func (h *Handler) getAirline(_ http.ResponseWriter, r *http.Request) (int, []mod
return page, al, nil
}

func (h *Handler) getAirlineDetails(_ http.ResponseWriter, r *http.Request) (models.Airline, error) {
func (h *Handler) getAirlineDetails(w http.ResponseWriter, r *http.Request) (models.Airline, error) {
vars := mux.Vars(r)
airlineName, ok := vars["airline_name"]
if !ok {
err := errors.New("airline_name not found in path")
HandleError(err, "Error fetching airline_name")
httperror.ErrNotFound.WriteError(w)
return models.Airline{}, err
}

c, err := h.service.GetAirlineByName(context.Background(), airlineName)
if err != nil {
HandleError(err, "Error fetching airline_name details")
httperror.ErrInvalidID.WriteError(w)
return models.Airline{}, err
}

Expand Down Expand Up @@ -104,6 +107,7 @@ func (h *Handler) renderAirlineTable(w http.ResponseWriter, r *http.Request) (te

if err != nil {
HandleError(err, "Error fetching airlines")
httperror.ErrNotFound.WriteError(w)
return nil, err
}

Expand All @@ -117,6 +121,7 @@ func (h *Handler) renderAirlineTable(w http.ResponseWriter, r *http.Request) (te

if err != nil {
HandleError(err, "error fetching total airline")
httperror.ErrNotFound.WriteError(w)
return nil, err
}

Expand All @@ -143,12 +148,14 @@ func (h *Handler) AirlineMainPage(w http.ResponseWriter, r *http.Request) error
var table, err = h.renderAirlineTable(w, r)
if err != nil {
HandleError(err, "Error rendering airline table")
httperror.ErrInternalServer.WriteError(w)
return err
}

al, err := h.service.GetAirlinesLocation()
if err != nil {
HandleError(err, "Error rendering airlines location")
httperror.ErrNotFound.WriteError(w)
return err
}

Expand All @@ -163,6 +170,7 @@ func (h *Handler) AirlineLocationPage(w http.ResponseWriter, r *http.Request) er
al, err := h.service.GetAirlinesLocation()
if err != nil {
HandleError(err, "Error rendering locations")
httperror.ErrInternalServer.WriteError(w)
return err
}
a := airline.AirlineLocationsPage(sidebar, al, "Airline", "Check airline expanded locations")
Expand All @@ -174,6 +182,7 @@ func (h *Handler) AirlineDetailsPage(w http.ResponseWriter, r *http.Request) err
al, err := h.getAirlineDetails(w, r)
if err != nil {
HandleError(err, "Error rendering details")
httperror.ErrNotFound.WriteError(w)
return err
}
a := airline.AirlineDetailsPage(sidebar, al, "Airline", "Check airport locations")
Expand Down Expand Up @@ -222,6 +231,7 @@ func (h *Handler) renderAirlineAircraftTable(w http.ResponseWriter, r *http.Requ
lastPage, err := h.service.GetAllAircraft()
if err != nil {
HandleError(err, "Error fetching last page")
httperror.ErrNotFound.WriteError(w)
return nil, err
}
data := models.AircraftTable{
Expand All @@ -243,7 +253,7 @@ func (h *Handler) renderAirlineAircraftTable(w http.ResponseWriter, r *http.Requ
return taxTable, nil
}

func (h *Handler) getAircraft(_ http.ResponseWriter, r *http.Request) (int, []models.Aircraft, error) {
func (h *Handler) getAircraft(w http.ResponseWriter, r *http.Request) (int, []models.Aircraft, error) {
pageSize := 20
orderBy := r.FormValue("orderBy")
sortBy := r.FormValue("sortBy")
Expand All @@ -254,14 +264,14 @@ func (h *Handler) getAircraft(_ http.ResponseWriter, r *http.Request) (int, []mo

page, err := strconv.Atoi(r.URL.Query().Get("page"))
if err != nil {
// Handle error or set a default page number
page = 1
}

a, err := h.service.GetAircraft(context.Background(), page, pageSize, aircraftName, orderBy, sortBy,
typeEngine, modelCode, planeOwner)
if err != nil {
HandleError(err, "Error fetching aircraft")
httperror.ErrNotFound.WriteError(w)
return 0, nil, err
}

Expand All @@ -272,6 +282,7 @@ func (h *Handler) AirlineAircraftPage(w http.ResponseWriter, r *http.Request) er
taxTable, err := h.renderAirlineAircraftTable(w, r)
sidebar := h.renderAirlineSidebar()
if err != nil {
httperror.ErrInternalServer.WriteError(w)
return err
}
a := airline.AirlineLayoutPage("Aircraft", "Check models about aircraft", taxTable, sidebar)
Expand All @@ -280,7 +291,7 @@ func (h *Handler) AirlineAircraftPage(w http.ResponseWriter, r *http.Request) er

// Airplane

func (h *Handler) getAirplane(_ http.ResponseWriter, r *http.Request) (int, []models.Airplane, error) {
func (h *Handler) getAirplane(w http.ResponseWriter, r *http.Request) (int, []models.Airplane, error) {
pageSize := 20
page, err := strconv.Atoi(r.URL.Query().Get("page"))
orderBy := r.FormValue("orderBy")
Expand All @@ -297,6 +308,7 @@ func (h *Handler) getAirplane(_ http.ResponseWriter, r *http.Request) (int, []mo
a, err := h.service.GetAirplanes(context.Background(), page, pageSize, orderBy, sortBy,
airlineName, modelName, productionLine, registrationNumber)
if err != nil {
httperror.ErrNotFound.WriteError(w)
return 0, nil, err
}

Expand Down Expand Up @@ -348,6 +360,7 @@ func (h *Handler) renderAirlineAirplaneTable(w http.ResponseWriter, r *http.Requ
lastPage, err := h.service.GetAllAirplanes()
if err != nil {
HandleError(err, "Error fetching last page")
httperror.ErrNotFound.WriteError(w)
return nil, err
}
a := models.AirplaneTable{
Expand All @@ -373,6 +386,7 @@ func (h *Handler) AirlineAirplanePage(w http.ResponseWriter, r *http.Request) er
a, err := h.renderAirlineAirplaneTable(w, r)
sidebar := h.renderAirlineSidebar()
if err != nil {
httperror.ErrInternalServer.WriteError(w)
return err
}
al := airline.AirlineLayoutPage("Airplane", "Check models about airplanes", a, sidebar)
Expand All @@ -381,7 +395,7 @@ func (h *Handler) AirlineAirplanePage(w http.ResponseWriter, r *http.Request) er

// Tax

func (h *Handler) getAirlineTax(_ http.ResponseWriter, r *http.Request) (int, []models.Tax, error) {
func (h *Handler) getAirlineTax(w http.ResponseWriter, r *http.Request) (int, []models.Tax, error) {
pageSize := 20
orderBy := r.FormValue("orderBy")
sortBy := r.FormValue("sortBy")
Expand All @@ -397,6 +411,7 @@ func (h *Handler) getAirlineTax(_ http.ResponseWriter, r *http.Request) (int, []

t, err := h.service.GetTax(context.Background(), page, pageSize, orderBy, sortBy, taxName, countryName, airlineName)
if err != nil {
httperror.ErrNotFound.WriteError(w)
return 0, nil, err
}

Expand Down Expand Up @@ -436,6 +451,7 @@ func (h *Handler) renderAirlineTaxTable(w http.ResponseWriter, r *http.Request)
lastPage, err := h.service.GetSum()
if err != nil {
HandleError(err, "Error fetching tax")
httperror.ErrNotFound.WriteError(w)
return nil, err
}
taxData := models.TaxTable{
Expand All @@ -460,6 +476,7 @@ func (h *Handler) AirlineTaxPage(w http.ResponseWriter, r *http.Request) error {
taxTable, err := h.renderAirlineTaxTable(w, r)
sidebar := h.renderAirlineSidebar()
if err != nil {
httperror.ErrNotFound.WriteError(w)
HandleError(err, "Error rendering table")
return err
}
Expand Down
21 changes: 14 additions & 7 deletions app/handlers/airport.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import (

"context"

httperror "github.com/FACorreiaa/Aviation-tracker/app/errors"
"github.com/FACorreiaa/Aviation-tracker/app/models"
svg2 "github.com/FACorreiaa/Aviation-tracker/app/static/svg"
airport "github.com/FACorreiaa/Aviation-tracker/app/view/airports"
"github.com/a-h/templ"
"github.com/gorilla/mux"
)

func (h *Handler) getAirports(_ http.ResponseWriter, r *http.Request) (int, []models.Airport, error) {
func (h *Handler) getAirports(w http.ResponseWriter, r *http.Request) (int, []models.Airport, error) {
pageSize := 20
page, err := strconv.Atoi(r.URL.Query().Get("page"))
orderBy := r.URL.Query().Get("orderBy")
Expand All @@ -27,18 +28,20 @@ func (h *Handler) getAirports(_ http.ResponseWriter, r *http.Request) (int, []mo

a, err := h.service.GetAirports(context.Background(), page, pageSize, orderBy, sortBy)
if err != nil {
httperror.ErrNotFound.WriteError(w)
return 0, nil, err
}

return page, a, nil
}

func (h *Handler) getAirportDetails(_ http.ResponseWriter, r *http.Request) (models.Airport, error) {
func (h *Handler) getAirportDetails(w http.ResponseWriter, r *http.Request) (models.Airport, error) {
vars := mux.Vars(r)
idStr, ok := vars["airport_id"]
if !ok {
err := errors.New("airport_id not found in path")
HandleError(err, "Error fetching airport_id")
httperror.ErrInternalServer.WriteError(w)
return models.Airport{}, err
}

Expand All @@ -51,13 +54,14 @@ func (h *Handler) getAirportDetails(_ http.ResponseWriter, r *http.Request) (mod
ap, err := h.service.GetAirportByID(context.Background(), id)
if err != nil {
HandleError(err, "Error fetching airport details")
httperror.ErrNotFound.WriteError(w)
return models.Airport{}, err
}

return ap, nil
}

func (h *Handler) getAirportByName(_ http.ResponseWriter, r *http.Request) (int, []models.Airport, error) {
func (h *Handler) getAirportByName(w http.ResponseWriter, r *http.Request) (int, []models.Airport, error) {
airportName := r.FormValue("airport_name")
countryName := r.FormValue("country_name")
gmt := r.FormValue("gmt")
Expand All @@ -71,6 +75,7 @@ func (h *Handler) getAirportByName(_ http.ResponseWriter, r *http.Request) (int,
}
ap, err := h.service.GetAirportByName(context.Background(), page, pageSize, orderBy, sortBy, airportName, countryName, gmt)
if err != nil {
httperror.ErrNotFound.WriteError(w)
return 0, nil, err
}
return page, ap, err
Expand Down Expand Up @@ -120,6 +125,7 @@ func (h *Handler) renderAirportTable(w http.ResponseWriter, r *http.Request) (te

lastPage, err := h.service.GetAllAirports()
if err != nil {
httperror.ErrInternalServer.WriteError(w)
HandleError(err, "Error fetching airports")
return nil, err
}
Expand Down Expand Up @@ -155,20 +161,19 @@ func (h *Handler) renderSidebar() []models.SidebarItem {
func (h *Handler) AirportPage(w http.ResponseWriter, r *http.Request) error {
at, err := h.renderAirportTable(w, r)
if err != nil {
httperror.ErrInternalServer.WriteError(w)
HandleError(err, "Error fetching airport data table")
return err
}
al, err := h.service.GetAirportsLocation()
if err != nil {
httperror.ErrNotFound.WriteError(w)
HandleError(err, "Error getting airport locations")
return err
}

sidebar := h.renderSidebar()
if err != nil {
HandleError(err, "Error fetching airport data table")
return err
}

a := airport.AirportPage(at, sidebar, "Airports", "Check airport locations", al)
return h.CreateLayout(w, r, "Airport Page", a).Render(context.Background(), w)
}
Expand All @@ -177,6 +182,7 @@ func (h *Handler) AirportLocationPage(w http.ResponseWriter, r *http.Request) er
al, err := h.service.GetAirportsLocation()
sidebar := h.renderSidebar()
if err != nil {
httperror.ErrInternalServer.WriteError(w)
HandleError(err, "Error fetching airport location table")
return err
}
Expand All @@ -188,6 +194,7 @@ func (h *Handler) AirportDetailsPage(w http.ResponseWriter, r *http.Request) err
ad, err := h.getAirportDetails(w, r)
sidebar := h.renderSidebar()
if err != nil {
httperror.ErrInternalServer.WriteError(w)
HandleError(err, "Error fetching airport details page")
return err
}
Expand Down
Loading

0 comments on commit bd2f813

Please sign in to comment.