Skip to content

Commit

Permalink
Fix a misleading error if the backend does not have correct groups
Browse files Browse the repository at this point in the history
If the portal's default groups aren't present on the backend dev
environment the current ItemNotFound error is very confusing. Log
an error in this case to the console and return 500 instead of 404.

Fixes #1369
  • Loading branch information
mikesname committed Sep 7, 2021
1 parent 64cc3fb commit 7a0739a
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ case class ItemNotFound(
def this(id: String) = this(Some("id"), Some(id))
}

case class ServerError(error: String) extends RuntimeException(error) with DataServiceError

case class CriticalError(error: String) extends RuntimeException(error) with DataServiceError

case class BadJson(
Expand Down
10 changes: 7 additions & 3 deletions modules/portal/app/controllers/portal/account/Accounts.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import auth.oauth2.{OAuth2Config, UserData}
import auth.sso.{DiscourseSSO, DiscourseSSOError, DiscourseSSONotEnabledError}
import auth.{AuthenticationError, HashedPassword}
import com.google.common.net.HttpHeaders
import controllers.{AppComponents, renderError}
import controllers.base.RecaptchaHelper
import controllers.portal.base.PortalController
import controllers.{AppComponents, renderError}
import forms.{AccountForms, HoneyPotForm, TimeCheckForm}
import models._
import play.api.Logger
Expand All @@ -21,7 +21,7 @@ import play.api.libs.openid.OpenIdClient
import play.api.libs.ws.WSClient
import play.api.mvc.{Result, _}
import services.RateLimitChecker
import services.data.{AnonymousUser, AuthenticatedUser}
import services.data.{AnonymousUser, AuthenticatedUser, ItemNotFound}
import services.oauth2.OAuth2Service

import java.net.ConnectException
Expand Down Expand Up @@ -151,7 +151,7 @@ case class Accounts @Inject()(
implicit val apiUser: AnonymousUser.type = AnonymousUser
val uuid = UUID.randomUUID()
val profileData = Map(UserProfileF.NAME -> data.name)
for {
(for {
profile <- userDataApi.createNewUserProfile[UserProfile](
data = profileData, groups = conf.defaultPortalGroups)
account <- accounts.create(Account(
Expand All @@ -166,6 +166,10 @@ case class Accounts @Inject()(
} yield {
sendValidationEmail(profile.data.name, data.email, uuid)
result
}).recoverWith {
case e: ItemNotFound =>
logger.error(s"Backend error creating user account: $e")
badForm(boundForm.withGlobalError("errors.databaseError"), InternalServerError)
}
}
}
Expand Down
1 change: 0 additions & 1 deletion modules/portal/conf/messages
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,6 @@ error.globalErrors=There were problems with that form:
error.date=Date is incorrect or badly formatted
error.badUrl=URL is incorrect or badly formatted. Note: for web addresses, a valid URL must be prefixed \
be the scheme, e.g. ''http://'' or ''https://''

error.required=Required
error.emailExists=Email is either invalid or is already registered
error.unknownUser=Unknown user
Expand Down
7 changes: 7 additions & 0 deletions test/integration/portal/SignupSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@ class SignupSpec extends IntegrationTestRunner {
}
}

"give a 500 error if defaultGroups not present in DB" in new ITestApp(
specificConfig = Map("ehri.portal.defaultUserGroups" -> Seq("BAD_GROUP"))) {
val signup = FakeRequest(accountRoutes.signupPost()).withCsrf.callWith(data)
status(signup) must equalTo(INTERNAL_SERVER_ERROR)
}


"allow log in after sign up" in new ITestApp {
val testEmail2 = "[email protected]"
val data2 = data.updated(SignupData.EMAIL, Seq(testEmail2))
Expand Down

0 comments on commit 7a0739a

Please sign in to comment.