@@ -21,19 +21,20 @@ import (
2121 "log/slog"
2222 "net/http"
2323
24- cwhttp "github.com/SencilloDev/sencillo-go/transports/http"
24+ sderrors "github.com/SencilloDev/sencillo-go/errors"
25+ sdhttp "github.com/SencilloDev/sencillo-go/transports/http"
2526)
2627
2728type clientHandlerFunc func (http.ResponseWriter , * http.Request , ClientManager ) error
2829
2930func getErrorDetails (err error ) (int , string ) {
30- clientError , ok := err .(* cwhttp .ClientError )
31+ clientError , ok := err .(* sderrors .ClientError )
3132 if ! ok {
3233 log .Printf ("An error ocurred: %v" , err )
3334 return 500 , http .StatusText (http .StatusInternalServerError )
3435 }
3536
36- return clientError .Status , clientError .Details
37+ return clientError .Status , string ( clientError .Body ())
3738}
3839
3940func clientHandler (h clientHandlerFunc , cm ClientManager ) http.HandlerFunc {
@@ -69,34 +70,30 @@ func (a *Application) createProduct(w http.ResponseWriter, r *http.Request) erro
6970 return nil
7071}
7172
72- func getProductByID (w http.ResponseWriter , r * http.Request , pm ProductManager ) {
73+ func getProductByID (w http.ResponseWriter , r * http.Request , pm ProductManager ) error {
7374 id := r .PathValue ("id" )
7475
7576 p := GetProduct (id , pm )
7677
77- if err := json .NewEncoder (w ).Encode (p ); err != nil {
78- log .Println (err )
79- }
78+ return json .NewEncoder (w ).Encode (p )
8079}
8180
82- func getProducts (w http.ResponseWriter , r * http.Request , pm ProductManager ) {
81+ func getProducts (w http.ResponseWriter , r * http.Request , pm ProductManager ) error {
8382 p := GetAllProducts (pm )
8483
85- if err := json .NewEncoder (w ).Encode (p ); err != nil {
86- log .Println (err )
87- }
84+ return json .NewEncoder (w ).Encode (p )
8885}
8986
9087func createClient (w http.ResponseWriter , r * http.Request , cm ClientManager ) error {
9188 var c Client
9289
9390 if err := json .NewDecoder (r .Body ).Decode (& c ); err != nil {
94- return cwhttp .NewClientError (err , http .StatusBadRequest )
91+ return sderrors .NewClientError (err , http .StatusBadRequest )
9592 }
9693
9794 a , err := NewClient (c .Name , SetClientProducts (c .Products ))
9895 if err != nil {
99- return cwhttp .NewClientError (err , http .StatusBadRequest )
96+ return sderrors .NewClientError (err , http .StatusBadRequest )
10097 }
10198
10299 if err := a .Save (cm ); err != nil {
@@ -131,22 +128,22 @@ func getClientByID(w http.ResponseWriter, r *http.Request, cm ClientManager) err
131128 return nil
132129}
133130
134- func (a * Application ) buildRoutes (l * slog.Logger ) []cwhttp .Route {
135- return []cwhttp .Route {
131+ func (a * Application ) buildRoutes (l * slog.Logger ) []sdhttp .Route {
132+ return []sdhttp .Route {
136133 {
137134 Method : http .MethodGet ,
138135 Path : "/products/{id}" ,
139- Handler : cwhttp . HandleWithContext (getProductByID , a .ProductManager ),
136+ Handler : sdhttp . HandleWithContextError (getProductByID , a .ProductManager , l ),
140137 },
141138 {
142139 Method : http .MethodGet ,
143140 Path : "/products" ,
144- Handler : cwhttp . HandleWithContext (getProducts , a .ProductManager ),
141+ Handler : sdhttp . HandleWithContextError (getProducts , a .ProductManager , l ),
145142 },
146143 {
147144 Method : http .MethodPost ,
148145 Path : "/clients" ,
149- Handler : clientHandler (createClient , a .ClientManager ),
146+ Handler : sdhttp . HandleWithContextError (createClient , a .ClientManager , l ),
150147 },
151148 {
152149 Method : http .MethodGet ,
@@ -161,7 +158,7 @@ func (a *Application) buildRoutes(l *slog.Logger) []cwhttp.Route {
161158 {
162159 Method : http .MethodPost ,
163160 Path : "/products" ,
164- Handler : & cwhttp .ErrHandler {
161+ Handler : & sdhttp .ErrHandler {
165162 Handler : a .createProduct ,
166163 Logger : l ,
167164 },
0 commit comments