@@ -98,20 +98,20 @@ type RegisterRequest struct {
9898func (h * Handlers ) Register (c echo.Context ) error {
9999 var req RegisterRequest
100100 if err := c .Bind (& req ); err != nil {
101- return c .String (http .StatusBadRequest , " invalid request body" )
101+ return c .JSON (http .StatusBadRequest , map [ string ] string { "error" : " invalid request body"} )
102102 }
103103
104104 // parse and validate request
105105 operator , err := did .Parse (req .Operator )
106106 if err != nil {
107- return c .String (http .StatusBadRequest , " invalid DID" )
107+ return c .JSON (http .StatusBadRequest , map [ string ] string { "error" : " invalid DID"} )
108108 }
109109 if ! common .IsHexAddress (req .OwnerAddress ) {
110- return c .String (http .StatusBadRequest , " invalid OwnerAddress" )
110+ return c .JSON (http .StatusBadRequest , map [ string ] string { "error" : " invalid owner address" } )
111111 }
112112 endpoint , err := url .Parse (req .PublicURL )
113113 if err != nil {
114- return c .String (http .StatusBadRequest , " invalid PublicURL" )
114+ return c .JSON (http .StatusBadRequest , map [ string ] string { "error" : " invalid public URL" } )
115115 }
116116
117117 if err := h .service .Register (c .Request ().Context (), registrar.RegisterParams {
@@ -122,24 +122,30 @@ func (h *Handlers) Register(c echo.Context) error {
122122 PublicURL : * endpoint ,
123123 Proof : req .Proof ,
124124 }); err != nil {
125- if errors .Is (err , registrar .ErrContractProviderNotRegistered ) {
126- return c .String (http .StatusUnprocessableEntity , "Provider not registered with smart-contract, must register first" )
127- }
128- if errors .Is (err , registrar .ErrDIDNotAllowed ) {
129- return c .String (http .StatusForbidden , "DID not allowed to register, contact Storacha team for help registering" )
130- }
131- if errors .Is (err , registrar .ErrDIDAlreadyRegistered ) {
132- return c .String (http .StatusConflict , "DID already registered" )
133- }
134- if errors .Is (err , registrar .ErrBadEndpoint ) {
135- return c .String (http .StatusBadRequest , "invalid PublicURL" )
136- }
137- if errors .Is (err , registrar .ErrInvalidProof ) {
138- return c .String (http .StatusBadRequest , "invalid Proof" )
125+ var status int
126+ var message string
127+ switch {
128+ case errors .Is (err , registrar .ErrContractProviderNotRegistered ):
129+ status = http .StatusUnprocessableEntity
130+ message = "provider not registered with smart-contract, must register first"
131+ case errors .Is (err , registrar .ErrDIDNotAllowed ):
132+ status = http .StatusForbidden
133+ message = "DID not allowed to register, contact Storacha team for help registering"
134+ case errors .Is (err , registrar .ErrDIDAlreadyRegistered ):
135+ status = http .StatusConflict
136+ message = "DID already registered"
137+ case errors .Is (err , registrar .ErrBadEndpoint ):
138+ status = http .StatusBadRequest
139+ message = "invalid public URL"
140+ case errors .Is (err , registrar .ErrInvalidProof ):
141+ status = http .StatusBadRequest
142+ message = "invalid proof"
143+ default :
144+ status = http .StatusInternalServerError
145+ message = err .Error ()
139146 }
140- return c .JSON (http .StatusInternalServerError , map [string ]string {
141- "error" : err .Error (),
142- })
147+ log .Error ("failed to register" , "operator" , operator , "error" , err )
148+ return c .JSON (status , map [string ]string {"error" : message })
143149 }
144150
145151 return c .NoContent (http .StatusCreated )
@@ -165,12 +171,12 @@ type Proofs struct {
165171func (h * Handlers ) RequestProofs (c echo.Context ) error {
166172 var req RequestProofsRequest
167173 if err := c .Bind (& req ); err != nil {
168- return c .String (http .StatusBadRequest , " invalid request body" )
174+ return c .JSON (http .StatusBadRequest , map [ string ] string { "error" : " invalid request body"} )
169175 }
170176
171177 operator , err := did .Parse (req .DID )
172178 if err != nil {
173- return c .String (http .StatusBadRequest , " invalid DID" )
179+ return c .JSON (http .StatusBadRequest , map [ string ] string { "error" : " invalid DID"} )
174180 }
175181
176182 indexerProof , egressTrackerProof , err := h .service .RequestProofs (c .Request ().Context (), operator )
@@ -188,12 +194,16 @@ func (h *Handlers) RequestProofs(c echo.Context) error {
188194
189195 indexerProofStr , err := delegation .Format (indexerProof )
190196 if err != nil {
191- return c .String (http .StatusInternalServerError , "failed to read generated indexer proof" )
197+ return c .JSON (http .StatusInternalServerError , map [string ]string {
198+ "error" : "failed to read generated indexer proof" ,
199+ })
192200 }
193201
194202 egressTrackerProofStr , err := delegation .Format (egressTrackerProof )
195203 if err != nil {
196- return c .String (http .StatusInternalServerError , "failed to read generated egress tracker proof" )
204+ return c .JSON (http .StatusInternalServerError , map [string ]string {
205+ "error" : "failed to read generated egress tracker proof" ,
206+ })
197207 }
198208
199209 return c .JSON (http .StatusOK , RequestProofsResponse {Proofs : Proofs {
@@ -209,12 +219,12 @@ type IsRegisteredRequest struct {
209219func (h * Handlers ) IsRegistered (c echo.Context ) error {
210220 var req IsRegisteredRequest
211221 if err := c .Bind (& req ); err != nil {
212- return c .String (http .StatusBadRequest , " invalid request body" )
222+ return c .JSON (http .StatusBadRequest , map [ string ] string { "error" : " invalid request body"} )
213223 }
214224
215225 operator , err := did .Parse (req .DID )
216226 if err != nil {
217- return c .String (http .StatusBadRequest , " invalid DID" )
227+ return c .JSON (http .StatusBadRequest , map [ string ] string { "error" : " invalid DID"} )
218228 }
219229
220230 registered , err := h .service .IsRegisteredDID (c .Request ().Context (), operator )
0 commit comments