Skip to content

Commit

Permalink
Add codes to error messages, make group names unique. Merge #49
Browse files Browse the repository at this point in the history
  • Loading branch information
zyro committed Mar 19, 2017
1 parent 14cbcc7 commit a3f6988
Show file tree
Hide file tree
Showing 16 changed files with 316 additions and 265 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ The format is based on [keep a changelog](http://keepachangelog.com/) and this p

### Changed
- The build system now strips up to current dir in recorded source file paths at compile.
- Addition of specific error messages to the Error payload.
- Group names must be unique.

### Fixed
- Fix regression loading config file.
Expand Down
4 changes: 2 additions & 2 deletions migrations/20170115200001_initial_schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
CREATE TABLE IF NOT EXISTS users (
PRIMARY KEY (id),
id BYTEA NOT NULL,
handle VARCHAR(20) UNIQUE NOT NULL,
handle VARCHAR(20) CONSTRAINT users_handle_key UNIQUE NOT NULL,
fullname VARCHAR(70),
avatar_url VARCHAR(255),
-- https://tools.ietf.org/html/bcp47
Expand Down Expand Up @@ -77,7 +77,7 @@ CREATE TABLE IF NOT EXISTS groups (
PRIMARY KEY (id),
id BYTEA NOT NULL,
creator_id BYTEA NOT NULL,
name VARCHAR(70) NOT NULL,
name VARCHAR(70) CONSTRAINT groups_name_key UNIQUE NOT NULL,
description VARCHAR(255),
avatar_url VARCHAR(255),
-- https://tools.ietf.org/html/bcp47
Expand Down
1 change: 1 addition & 0 deletions migrations/20170228205100_leaderboards.sql
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ CREATE TABLE IF NOT EXISTS leaderboard_record (
ranked_at INT CHECK (ranked_at >= 0) DEFAULT 0 NOT NULL,
updated_at INT CHECK (updated_at > 0) NOT NULL,
-- Used to enable proper order in revscan when sorting by score descending.
-- Revscan is unaviodable here due to cockroachdb/cockroach#14241.
updated_at_inverse INT CHECK (updated_at > 0) NOT NULL,
expires_at INT CHECK (expires_at >= 0) DEFAULT 0 NOT NULL,
banned_at INT CHECK (expires_at >= 0) DEFAULT 0 NOT NULL
Expand Down
23 changes: 20 additions & 3 deletions server/api.proto
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,23 @@ message Heartbeat {
}

message Error {
string reason = 1;
enum Code {
RUNTIME_EXCEPTION = 0;
UNRECOGNIZED_PAYLOAD = 1;
MISSING_PAYLOAD = 2;
BAD_INPUT = 3;
AUTH_ERROR = 4;
USER_LINK_INUSE = 5;
USER_LINK_PROVIDER_UNAVAILABLE = 6;
USER_UNLINK_DISALLOWED = 7;
USER_HANDLE_INUSE = 8;
GROUP_NAME_INUSE = 9;
STORAGE_FETCH_DISALLOWED = 10;
MATCH_NOT_FOUND = 11;
}

int32 code = 1;
string message = 2;
}

message AuthenticateRequest {
Expand Down Expand Up @@ -58,8 +74,9 @@ message AuthenticateResponse {
}

message Error {
string reason = 1;
AuthenticateRequest request = 2;
int32 code = 1;
string message = 2;
AuthenticateRequest request = 3;
}

string collation_id = 1;
Expand Down
21 changes: 19 additions & 2 deletions server/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,25 @@ func (p *pipeline) processRequest(logger zap.Logger, session *session, envelope
p.leaderboardRecordsList(logger, session, envelope)

case nil:
session.Send(&Envelope{CollationId: envelope.CollationId, Payload: &Envelope_Error{&Error{Reason: "No payload found"}}})
session.Send(ErrorMessage(envelope.CollationId, MISSING_PAYLOAD, "No payload found"))
default:
session.Send(&Envelope{CollationId: envelope.CollationId, Payload: &Envelope_Error{&Error{Reason: "Unrecognized payload"}}})
session.Send(ErrorMessage(envelope.CollationId, UNRECOGNIZED_PAYLOAD, "Unrecognized payload"))
}
}

func ErrorMessageRuntimeException(collationID string, message string) *Envelope {
return ErrorMessage(collationID, RUNTIME_EXCEPTION, message)
}

func ErrorMessageBadInput(collationID string, message string) *Envelope {
return ErrorMessage(collationID, BAD_INPUT, message)
}

func ErrorMessage(collationID string, code Error_Code, message string) *Envelope {
return &Envelope{
CollationId: collationID,
Payload: &Envelope_Error{&Error{
Message: message,
Code: int32(code),
}}}
}
40 changes: 20 additions & 20 deletions server/pipeline_friend.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,29 +198,29 @@ FROM users, user_edge ` + filterQuery
func (p *pipeline) friendAdd(l zap.Logger, session *session, envelope *Envelope) {
addFriendRequest := envelope.GetFriendAdd()
if len(addFriendRequest.UserId) == 0 {
session.Send(&Envelope{CollationId: envelope.CollationId, Payload: &Envelope_Error{&Error{Reason: "User ID must be present"}}})
session.Send(ErrorMessageBadInput(envelope.CollationId, "User ID must be present"))
return
}

friendID, err := uuid.FromBytes(addFriendRequest.UserId)
if err != nil {
l.Warn("Could not add friend", zap.Error(err))
session.Send(&Envelope{CollationId: envelope.CollationId, Payload: &Envelope_Error{&Error{Reason: "Invalid User ID"}}})
session.Send(ErrorMessageBadInput(envelope.CollationId, "Invalid User ID"))
return
}
logger := l.With(zap.String("friend_id", friendID.String()))
friendIDBytes := friendID.Bytes()

if friendID.String() == session.userID.String() {
logger.Warn("Cannot add self", zap.Error(err))
session.Send(&Envelope{CollationId: envelope.CollationId, Payload: &Envelope_Error{&Error{Reason: "Cannot add self"}}})
session.Send(ErrorMessageBadInput(envelope.CollationId, "Cannot add self"))
return
}

tx, err := p.db.Begin()
if err != nil {
logger.Error("Could not add friend", zap.Error(err))
session.Send(&Envelope{CollationId: envelope.CollationId, Payload: &Envelope_Error{&Error{Reason: "Failed to add friend"}}})
session.Send(ErrorMessageRuntimeException(envelope.CollationId, "Failed to add friend"))
return
}
defer func() {
Expand All @@ -231,12 +231,12 @@ func (p *pipeline) friendAdd(l zap.Logger, session *session, envelope *Envelope)
logger.Error("Could not rollback transaction", zap.Error(err))
}

session.Send(&Envelope{CollationId: envelope.CollationId, Payload: &Envelope_Error{&Error{Reason: "Failed to add friend"}}})
session.Send(ErrorMessageRuntimeException(envelope.CollationId, "Failed to add friend"))
} else {
err = tx.Commit()
if err != nil {
logger.Error("Could not commit transaction", zap.Error(err))
session.Send(&Envelope{CollationId: envelope.CollationId, Payload: &Envelope_Error{&Error{Reason: "Failed to add friend"}}})
session.Send(ErrorMessageRuntimeException(envelope.CollationId, "Failed to add friend"))
} else {
logger.Info("Added friend")
session.Send(&Envelope{CollationId: envelope.CollationId})
Expand Down Expand Up @@ -267,7 +267,7 @@ WHERE EXISTS (SELECT id FROM users WHERE id=$2)

rowsAffected, _ = res.RowsAffected()
if rowsAffected == 0 {
err = errors.New("Did not find friend ID in users table.")
err = errors.New("did not find friend ID in users table")
return
}

Expand All @@ -292,29 +292,29 @@ WHERE EXISTS (SELECT id FROM users WHERE id=$2)
func (p *pipeline) friendRemove(l zap.Logger, session *session, envelope *Envelope) {
removeFriendRequest := envelope.GetFriendRemove()
if len(removeFriendRequest.UserId) == 0 {
session.Send(&Envelope{CollationId: envelope.CollationId, Payload: &Envelope_Error{&Error{Reason: "User ID must be present"}}})
session.Send(ErrorMessageBadInput(envelope.CollationId, "User ID must be present"))
return
}

friendID, err := uuid.FromBytes(removeFriendRequest.UserId)
if err != nil {
l.Warn("Could not add friend", zap.Error(err))
session.Send(&Envelope{CollationId: envelope.CollationId, Payload: &Envelope_Error{&Error{Reason: "Invalid User ID"}}})
session.Send(ErrorMessageBadInput(envelope.CollationId, "Invalid User ID"))
return
}
logger := l.With(zap.String("friend_id", friendID.String()))
friendIDBytes := friendID.Bytes()

if friendID.String() == session.userID.String() {
logger.Warn("Cannot remove self", zap.Error(err))
session.Send(&Envelope{CollationId: envelope.CollationId, Payload: &Envelope_Error{&Error{Reason: "Cannot remove self"}}})
session.Send(ErrorMessageBadInput(envelope.CollationId, "Cannot remove self"))
return
}

tx, err := p.db.Begin()
if err != nil {
logger.Error("Could not remove friend", zap.Error(err))
session.Send(&Envelope{CollationId: envelope.CollationId, Payload: &Envelope_Error{&Error{Reason: "Failed to remove friend"}}})
session.Send(ErrorMessageRuntimeException(envelope.CollationId, "Failed to remove friend"))
return
}
defer func() {
Expand All @@ -325,12 +325,12 @@ func (p *pipeline) friendRemove(l zap.Logger, session *session, envelope *Envelo
logger.Error("Could not rollback transaction", zap.Error(err))
}

session.Send(&Envelope{CollationId: envelope.CollationId, Payload: &Envelope_Error{&Error{Reason: "Failed to remove friend"}}})
session.Send(ErrorMessageRuntimeException(envelope.CollationId, "Failed to remove friend"))
} else {
err = tx.Commit()
if err != nil {
logger.Error("Could not commit transaction", zap.Error(err))
session.Send(&Envelope{CollationId: envelope.CollationId, Payload: &Envelope_Error{&Error{Reason: "Failed to remove friend"}}})
session.Send(ErrorMessageRuntimeException(envelope.CollationId, "Failed to remove friend"))
} else {
logger.Info("Removed friend")
session.Send(&Envelope{CollationId: envelope.CollationId})
Expand Down Expand Up @@ -360,29 +360,29 @@ func (p *pipeline) friendRemove(l zap.Logger, session *session, envelope *Envelo
func (p *pipeline) friendBlock(l zap.Logger, session *session, envelope *Envelope) {
blockUserRequest := envelope.GetFriendBlock()
if len(blockUserRequest.UserId) == 0 {
session.Send(&Envelope{CollationId: envelope.CollationId, Payload: &Envelope_Error{&Error{Reason: "User ID must be present"}}})
session.Send(ErrorMessageBadInput(envelope.CollationId, "User ID must be present"))
return
}

userID, err := uuid.FromBytes(blockUserRequest.UserId)
if err != nil {
l.Warn("Could not block user", zap.Error(err))
session.Send(&Envelope{CollationId: envelope.CollationId, Payload: &Envelope_Error{&Error{Reason: "Invalid User ID"}}})
session.Send(ErrorMessageBadInput(envelope.CollationId, "Invalid User ID"))
return
}
logger := l.With(zap.String("user_id", userID.String()))
userIDBytes := userID.Bytes()

if userID.String() == session.userID.String() {
logger.Warn("Cannot block self", zap.Error(err))
session.Send(&Envelope{CollationId: envelope.CollationId, Payload: &Envelope_Error{&Error{Reason: "Cannot block self"}}})
session.Send(ErrorMessageBadInput(envelope.CollationId, "Cannot block self"))
return
}

tx, err := p.db.Begin()
if err != nil {
logger.Error("Could not block user", zap.Error(err))
session.Send(&Envelope{CollationId: envelope.CollationId, Payload: &Envelope_Error{&Error{Reason: "Failed to remove friend"}}})
session.Send(ErrorMessageRuntimeException(envelope.CollationId, "Failed to block friend"))
return
}
defer func() {
Expand All @@ -397,12 +397,12 @@ func (p *pipeline) friendBlock(l zap.Logger, session *session, envelope *Envelop
logger.Error("Could not rollback transaction", zap.Error(err))
}

session.Send(&Envelope{CollationId: envelope.CollationId, Payload: &Envelope_Error{&Error{Reason: "Could not block user"}}})
session.Send(ErrorMessageRuntimeException(envelope.CollationId, "Could not block user"))
} else {
err = tx.Commit()
if err != nil {
logger.Error("Could not commit transaction", zap.Error(err))
session.Send(&Envelope{CollationId: envelope.CollationId, Payload: &Envelope_Error{&Error{Reason: "Could not block user"}}})
session.Send(ErrorMessageRuntimeException(envelope.CollationId, "Could not block user"))
} else {
logger.Info("User blocked")
session.Send(&Envelope{CollationId: envelope.CollationId})
Expand Down Expand Up @@ -439,7 +439,7 @@ func (p *pipeline) friendsList(logger zap.Logger, session *session, envelope *En
friends, err := p.getFriends("WHERE id = destination_id AND source_id = $1", session.userID.Bytes())
if err != nil {
logger.Error("Could not get friends", zap.Error(err))
session.Send(&Envelope{CollationId: envelope.CollationId, Payload: &Envelope_Error{&Error{Reason: "Could not get friends"}}})
session.Send(ErrorMessageRuntimeException(envelope.CollationId, "Could not get friends"))
return
}

Expand Down
Loading

0 comments on commit a3f6988

Please sign in to comment.