Lasa is a stateless proxy that generates a RSS or an Atom feed from a Standard.site publication. lasa.anhgelus.world
rss atom atprotocol standard-site atproto
2
fork

Configure Feed

Select the types of activity you want to include in your feed.

refactor(server): generalize error handling

+17 -36
+7 -21
cmd/lasad/server/directory.go
··· 3 3 import ( 4 4 "bytes" 5 5 "context" 6 - "errors" 7 6 "fmt" 8 7 "html/template" 9 8 "io" ··· 122 121 client := ctx.Value(keyClient).(xrpc.Client) 123 122 did, err := lasa.Resolve(ctx, client.Directory(), r.PathValue("id")) 124 123 if err != nil { 125 - w.WriteHeader(http.StatusBadRequest) 126 - return nil 124 + return err 127 125 } 128 126 rkey, err := atproto.ParseRecordKey(r.PathValue("rkey")) 129 127 if err != nil { 130 - w.WriteHeader(http.StatusBadRequest) 131 - return nil 128 + return err 132 129 } 133 130 key := did.String() + ":" + rkey.String() + ":" + kind 134 131 b := d.fromCache(ctx, key) ··· 140 137 slog.Debug("cannot get feed from cache", "did", did, "kind", kind) 141 138 142 139 b, err = d.limiter.Do(key, func() ([]byte, error) { 143 - pub, ok, err := getPub(ctx, did, rkey) 144 - if !ok { 140 + pub, err := getPub(ctx, did, rkey) 141 + if err != nil { 145 142 return nil, err 146 143 } 147 144 var bf bytes.Buffer ··· 158 155 }) 159 156 if err != nil { 160 157 return err 161 - } 162 - if b == nil { 163 - w.WriteHeader(http.StatusNotFound) 164 - return nil 165 158 } 166 159 w.Write(b) 167 160 return err 168 161 } 169 162 170 - func getPub(ctx context.Context, did *atproto.DID, rkey atproto.RecordKey) (xrpc.RecordStored[*site.Publication], bool, error) { 163 + func getPub(ctx context.Context, did *atproto.DID, rkey atproto.RecordKey) (xrpc.RecordStored[*site.Publication], error) { 171 164 client := ctx.Value(keyClient).(xrpc.Client) 172 165 pub, err := xrpc.GetRecord[*site.Publication](ctx, client, did, rkey, nil) 173 166 if err != nil { 174 - if err, ok := errors.AsType[xrpc.ErrStandardResponse](err); ok { 175 - if errors.Is(err, xrpc.ErrRecordNotFound) { 176 - return pub, false, nil 177 - } 178 - return pub, false, err 179 - } else { 180 - return pub, false, err 181 - } 167 + return pub, err 182 168 } 183 - return pub, true, nil 169 + return pub, nil 184 170 }
+10 -15
cmd/lasad/server/errors.go
··· 9 9 ) 10 10 11 11 func HandleErrors(w http.ResponseWriter, err error) { 12 - w.Header().Add("Content-Type", "text/plain") 12 + var status int 13 13 if atproto.IsErrCannotParse(err) { 14 - w.WriteHeader(http.StatusBadRequest) 15 - w.Write([]byte(err.Error())) 16 - return 14 + status = http.StatusBadRequest 17 15 } else if errors.Is(err, atproto.ErrHandleNotFound) { 18 - errorNotFound(w, err) 19 - return 20 - } else if e, ok := errors.AsType[atproto.ErrDIDNotFound](err); ok { 21 - errorNotFound(w, e) 22 - return 23 - } else if e, ok := errors.AsType[xrpc.ErrStandardResponse](err); ok && errors.Is(err, xrpc.ErrRecordNotFound) { 24 - errorNotFound(w, e) 16 + status = http.StatusNotFound 17 + } else if _, ok := errors.AsType[atproto.ErrDIDNotFound](err); ok { 18 + status = http.StatusNotFound 19 + } else if errors.Is(err, xrpc.ErrRecordNotFound) { 20 + status = http.StatusNotFound 21 + } 22 + if status > 0 { 23 + http.Error(w, err.Error(), status) 25 24 return 26 25 } 27 26 panic(err) 28 - } 29 27 30 - func errorNotFound(w http.ResponseWriter, err error) { 31 - w.WriteHeader(http.StatusNotFound) 32 - w.Write([]byte(err.Error())) 33 28 }