diff --git a/api/account.go b/api/account.go index 89c2e31..84ad41f 100644 --- a/api/account.go +++ b/api/account.go @@ -27,6 +27,11 @@ func (server *Server) createAccount(ctx *gin.Context) { } account, err := server.store.CreateAccount(ctx, arg) if err != nil { + errCode := db.ErrorCode(err) + if errCode == db.ForeignKeyViolation || errCode == db.UniqueViolation { + ctx.JSON(http.StatusForbidden, errorResponse(err)) + return + } ctx.JSON(http.StatusInternalServerError, errorResponse(err)) return } diff --git a/api/account_test.go b/api/account_test.go index 9209abc..12a7866 100644 --- a/api/account_test.go +++ b/api/account_test.go @@ -17,7 +17,8 @@ import ( ) func TestGetAccountAPI(t *testing.T) { - account := randomAccount() + user, _ := randomUser(t) + account := randomAccount(user.Username) testCases := []struct { name string @@ -89,10 +90,10 @@ func TestGetAccountAPI(t *testing.T) { } } -func randomAccount() db.Account { +func randomAccount(username string) db.Account { return db.Account{ ID: util.RandomInt(1, 1000), - Owner: util.RandomOwner(), + Owner: username, Balance: util.RandomMoney(), Currency: util.RandomCurrency(), }