Skip to content

Commit

Permalink
Update error codes for login/register/link scenarios. Merge #95
Browse files Browse the repository at this point in the history
  • Loading branch information
zyro committed Jul 18, 2017
1 parent f02f02d commit e781731
Show file tree
Hide file tree
Showing 4 changed files with 211 additions and 159 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,16 @@ The format is based on [keep a changelog](http://keepachangelog.com/) and this p
- Streamline command line flags to be inline with the config file.
- Restructure and stabilize API messages.
- Update Runtime modules to use plural function names for batch operations. (`users_fetch_id` and `users_fetch_handle`)
- Script runtime JSON encoder/decoder now support non-object JSON documents.
- Script runtime storage bindings now expect and return Lua tables for values.
- Attempting to login with an ID that does not exist will return a new dedicated error code.
- Attempting to register with an ID that already exists will return a new dedicated error code.

### Fixed
- Invocation type was always set to "Before" in After Runtime scripts.
- User ID was not passed to context in After Authentication invocations.
- Authentication runtime invocation messages were named with leading "." and trailing "_".
- Attempting to link a device ID that is already in use will return the correct "link in use" error code.

## [0.13.1] - 2017-06-08
### Added
Expand Down
22 changes: 13 additions & 9 deletions server/api.proto
Original file line number Diff line number Diff line change
Expand Up @@ -52,24 +52,28 @@ message Error {
BAD_INPUT = 3;
/// Authentication failure.
AUTH_ERROR = 4;
/// Login failed because ID/device/email did not exist.
USER_NOT_FOUND = 5;
/// Registration failed because ID/device/email exists.
USER_REGISTER_INUSE = 6;
/// Linking operation failed because link exists.
USER_LINK_INUSE = 5;
USER_LINK_INUSE = 7;
/// Linking operation failed because third-party service was unreachable.
USER_LINK_PROVIDER_UNAVAILABLE = 6;
USER_LINK_PROVIDER_UNAVAILABLE = 8;
/// Unlinking operation failed because you cannot unlink last ID.
USER_UNLINK_DISALLOWED = 7;
USER_UNLINK_DISALLOWED = 9;
/// Handle is in-use by another user.
USER_HANDLE_INUSE = 8;
USER_HANDLE_INUSE = 10;
/// Group names must be unique and it's already in use.
GROUP_NAME_INUSE = 9;
GROUP_NAME_INUSE = 11;
/// Storage write operation failed.
STORAGE_REJECTED = 10;
STORAGE_REJECTED = 12;
/// Match with given ID was not found in the system.
MATCH_NOT_FOUND = 11;
MATCH_NOT_FOUND = 13;
/// Runtime function name was not found in system registry.
RUNTIME_FUNCTION_NOT_FOUND = 12;
RUNTIME_FUNCTION_NOT_FOUND = 14;
/// Runtime function caused an internal server error and did not complete.
RUNTIME_FUNCTION_EXCEPTION = 13;
RUNTIME_FUNCTION_EXCEPTION = 15;
}

/// Error code - must be one of the Error.Code enums above.
Expand Down
17 changes: 11 additions & 6 deletions server/pipeline_link_unlink.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,17 @@ func (p *pipeline) linkDevice(logger *zap.Logger, session *session, envelope *En
}
res, err := txn.Exec("INSERT INTO user_device (id, user_id) VALUES ($1, $2)", deviceID, session.userID.Bytes())
if err != nil {
logger.Warn("Could not link, query error", zap.Error(err))
err = txn.Rollback()
if err != nil {
logger.Warn("Could not link, transaction rollback error", zap.Error(err))
// In any error case the link has failed, so we can rollback before checking what went wrong.
if e := txn.Rollback(); e != nil {
logger.Warn("Could not link, transaction rollback error", zap.Error(e))
}

if strings.HasSuffix(err.Error(), "violates unique constraint \"primary\"") {
session.Send(ErrorMessage(envelope.CollationId, USER_LINK_INUSE, "Device ID in use"))
} else {
logger.Warn("Could not link, query error", zap.Error(err))
session.Send(ErrorMessageRuntimeException(envelope.CollationId, "Could not link"))
}
session.Send(ErrorMessageRuntimeException(envelope.CollationId, "Could not link"))
return
}
if count, _ := res.RowsAffected(); count == 0 {
Expand Down Expand Up @@ -104,7 +109,7 @@ func (p *pipeline) linkDevice(logger *zap.Logger, session *session, envelope *En
}
err = txn.Commit()
if err != nil {
logger.Warn("Could not register, transaction commit error", zap.Error(err))
logger.Warn("Could not link, transaction commit error", zap.Error(err))
session.Send(ErrorMessageRuntimeException(envelope.CollationId, "Could not link"))
return
}
Expand Down
Loading

0 comments on commit e781731

Please sign in to comment.