api/server: better error checking to avoid unnecessary panics