wip: currently rewriting the project as a full stack application tangled.org/kacaii.dev/sigo
gleam
0
fork

Configure Feed

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

:bulb: add documentation to `UpdateBrigadeStatusError`

Kacaii 25b7d289 9ec50a0b

+35 -33
+31 -29
src/app/routes/brigade/update_brigade_status.gleam
··· 5 5 import gleam/http 6 6 import gleam/json 7 7 import gleam/list 8 - import gleam/result 8 + import gleam/result.{try} 9 9 import gleam/time/timestamp 10 10 import pog 11 11 import wisp ··· 27 27 id brigade_id: String, 28 28 ) -> wisp.Response { 29 29 use <- wisp.require_method(req, http.Put) 30 - use json_data <- wisp.require_json(req) 30 + use data <- wisp.require_json(req) 31 31 32 - case decode.run(json_data, is_active_decoder()) { 33 - Error(_) -> wisp.unprocessable_content() 34 - Ok(is_active) -> { 32 + let decoder = body_decoder() 33 + case decode.run(data, decoder) { 34 + Error(err) -> web.handle_decode_error(err) 35 + Ok(is_active) -> 35 36 case try_update_status(ctx, brigade_id, is_active) { 36 - Ok(resp) -> wisp.json_response(json.to_string(resp), 200) 37 + Ok(body) -> wisp.json_response(body, 200) 37 38 Error(err) -> handle_error(err) 38 39 } 39 - } 40 40 } 41 41 } 42 42 43 + ///  Updating a brigade status can fail 44 + type UpdateBrigadeStatusError { 45 + /// Brigade contains invalid Uuid 46 + InvalidUuid(String) 47 + /// An error occurred while accessing the DataBase 48 + DataBase(pog.QueryError) 49 + /// Brigade not found in the DataBase 50 + BrigadeNotFound(String) 51 + } 52 + 43 53 fn handle_error(err: UpdateBrigadeStatusError) -> wisp.Response { 44 54 case err { 45 - InvalidBrigadeUuid(id) -> 46 - wisp.unprocessable_content() 47 - |> wisp.set_body(wisp.Text( 48 - "Brigada de incêndio possui UUID Inválido: " <> id, 49 - )) 50 - UuidNotFound(id) -> wisp.bad_request("Equipe não encontrada: " <> id) 51 - DataBaseError(err) -> web.handle_database_error(err) 55 + InvalidUuid(id) -> wisp.bad_request("Equipe possui UUID Inválido: " <> id) 56 + BrigadeNotFound(id) -> wisp.bad_request("Equipe não encontrada: " <> id) 57 + DataBase(err) -> web.handle_database_error(err) 52 58 } 53 59 } 54 60 ··· 56 62 ctx: Context, 57 63 id: String, 58 64 is_active: Bool, 59 - ) -> Result(json.Json, UpdateBrigadeStatusError) { 60 - use brigade_uuid <- result.try( 61 - uuid.from_string(id) |> result.replace_error(InvalidBrigadeUuid(id)), 65 + ) -> Result(String, UpdateBrigadeStatusError) { 66 + use brigade_uuid <- try( 67 + uuid.from_string(id) |> result.replace_error(InvalidUuid(id)), 62 68 ) 63 - use returned <- result.try( 69 + 70 + use returned <- try( 64 71 sql.update_brigade_status(ctx.db, brigade_uuid, is_active) 65 - |> result.map_error(DataBaseError), 72 + |> result.map_error(DataBase), 66 73 ) 67 - use row <- result.try( 74 + 75 + use row <- result.map( 68 76 list.first(returned.rows) 69 - |> result.replace_error(UuidNotFound(id)), 77 + |> result.replace_error(BrigadeNotFound(id)), 70 78 ) 71 79 72 - Ok( 80 + json.to_string( 73 81 json.object([ 74 82 #("id", json.string(uuid.to_string(row.id))), 75 83 #("is_active", json.bool(row.is_active)), ··· 78 86 ) 79 87 } 80 88 81 - fn is_active_decoder() -> decode.Decoder(Bool) { 89 + fn body_decoder() -> decode.Decoder(Bool) { 82 90 use is_active <- decode.field("ativo", decode.bool) 83 91 decode.success(is_active) 84 92 } 85 - 86 - type UpdateBrigadeStatusError { 87 - InvalidBrigadeUuid(String) 88 - DataBaseError(pog.QueryError) 89 - UuidNotFound(String) 90 - }
+4 -4
src/app/web/socket.gleam
··· 212 212 let cookies = request.get_cookies(req) 213 213 let salt = <<ctx.secret_key_base:utf8>> 214 214 215 - use hashed_uuid <- result.try( 215 + use hashed_uuid <- try( 216 216 list.key_find(cookies, user.uuid_cookie_name) 217 217 |> result.replace_error(MissingCookie), 218 218 ) 219 219 220 - use decrypted <- result.try( 220 + use decrypted <- try( 221 221 crypto.verify_signed_message(hashed_uuid, salt) 222 222 |> result.replace_error(InvalidSignature), 223 223 ) 224 224 225 - use maybe_uuid_str <- result.try( 225 + use maybe_uuid_str <- try( 226 226 bit_array.to_string(decrypted) 227 227 |> result.replace_error(InvalidUtf8), 228 228 ) 229 229 230 - use user_uuid <- result.try( 230 + use user_uuid <- try( 231 231 uuid.from_string(maybe_uuid_str) 232 232 |> result.replace_error(InvalidUuid(maybe_uuid_str)), 233 233 )