Skip to content

Commit

Permalink
Better error handling in case of not unique component name
Browse files Browse the repository at this point in the history
  • Loading branch information
attdona committed Apr 19, 2024
1 parent b68748c commit b6eed13
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
16 changes: 13 additions & 3 deletions src/Rembus.jl
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ Base.@kwdef struct RembusError <: RembusException
reason::Union{String,Nothing} = nothing
end

struct AlreadyConnected <: RembusException
cid::String
end

"""
`RpcMethodNotFound` is thrown from a rpc request when the called method is unknown.
Expand Down Expand Up @@ -1044,6 +1048,12 @@ function rembus_task(pd, rb, protocol=:ws)
end
end
catch e
if isa(e, AlreadyConnected)
@error "[$(e.cid)] already connected"
Rembus.CONFIG.cid = "rembus"
return
end

if isa(e, HTTP.Exceptions.ConnectError)
msg = "[$pd]: $(e.url) connection error"
else
Expand All @@ -1065,8 +1075,7 @@ end

mutable struct NullProcess <: Visor.Supervised
id::String
inbo
x::Channel
inbox::Channel
NullProcess(id) = new(id, Channel(1))
end

Expand Down Expand Up @@ -1472,13 +1481,14 @@ function authenticate(rb)
if isa(response, RembusTimeout)
close(rb.socket)
throw(response)
elseif (response.status == STS_GENERIC_ERROR)
throw(AlreadyConnected(rb.client.id))
elseif (response.status == STS_CHALLENGE)
msg = attestate(rb, response)
response = wait_response(rb, msg, request_timeout())
end

if (response.status != STS_SUCCESS)
# Avoid DOS attack: this has to be the server work!!
close(rb.socket)
rembuserror(code=response.status, reason=reason)
end
Expand Down
5 changes: 4 additions & 1 deletion src/broker.jl
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,10 @@ function anonymous_twin_receiver(router, twin)
named = named_twin(msg.cid, router)
if named !== nothing && !offline(named)
@warn "a component with id [$(msg.cid)] is already connected"
close(ws)
## close(ws)
transport_send(twin, ws, ResMsg(
msg.id, STS_GENERIC_ERROR, "already connected"
))
else
# check if cid is registered
rembus_login = isfile(joinpath(CONFIG.db, "apps", msg.cid))
Expand Down

0 comments on commit b6eed13

Please sign in to comment.