Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8135975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T10:35:46+00:00 2026-06-06T10:35:46+00:00

I have a problem with my code. How can I handle an error from

  • 0

I have a problem with my code. How can I handle an error from service to my gsp? I tried it with render from service or controller, but instead something like [Property [{0}] of class [{1}] with value [{2}] is not a valid e-mail address] and got a Error 500: Internal Server Error with full exception trace. My sources:

UserController.groovy

def saveUser() {

    def salt = new SecureRandomNumberGenerator().nextBytes().getBytes()
    def user

    try {
        user = userService.registerMethod(params.username, params.passwordHash, params.passwordHash2,
            salt, params.firstName, params.lastName, params.sex, params.email, params.mobile, params.userType)

        session.user = user
        flash.message = "User ${user.profile.firstName} ${user.profile.lastName} was successfuly created!"
        redirect(uri:"/")
    } catch (UserRegistrationException ure) {
        user = ure.user
        if (user) {
            render(view:"registration",model:[user:user])
        } else {
            flash.message = ure.message
            redirect(action:"registration")
        }
    }

}

UserService.groovy

class UserRegistrationException extends RuntimeException {
String message
User user
}

class UserService {

boolean transactional = true

User registerMethod(String username, String password1, String password2, Object salt, String firstName, String lastName,
        String sex, String email, String mobile, String userType) {

    def user = User.findByUsername(username)
    if(user) {
        //flash.message = "User already exists with login ${username}"
        throw new UserRegistrationException(message:"User already exists with login ${username}")
        redirect(uri:"/")
    } else {
        if (password1 != password2) {
            //flash.message = "Passwords don't match"
            throw new UserRegistrationException(message:"Passwords don't match")
        } else {
            def profile = Profile.findByEmail(email)
            if (profile && (email == profile.email)) {
                //flash.message = "User with this email is already exists!"
                throw new UserRegistrationException(message:"User with this email is already exists!")
            } else {
                user = new User(
                    username:username,
                    passwordHash: new Sha512Hash(password1, salt, 1024).toHex(),
                    passwordSalt:salt,
                    profile: new Profile(
                        firstName:firstName,
                        lastName:lastName,
                        sex:sex,
                        email:email,
                        mobile:mobile,
                        userType:userType
                        )
                    )

                if (!user.hasErrors() && user.save(flush:true,failOnError:true)) {
                    def authToken = new UsernamePasswordToken(username, password1)
                    SecurityUtils.subject.login(authToken)
                    return user
                } else {
                    throw new UserRegistrationException(message:"Can't create user");
                }
            }
        }
    }
}

}

registration.gsp

<body>
    <h1>Registration Page</h1>

    <g:if test="${ flash.message }">
        ${ flash.message }
    </g:if>
    <g:hasErrors bean="${ user }">
        <g:renderErrors bean="${ user }" as="list"/>
    </g:hasErrors>

    <div id="registration">
        <g:form action="saveUser">
        <table>
            <tr>
                <td><label for="username">Login: </label></td>
                <td><g:textField name="username" id="username" /></td>
            </tr>
            <tr>
                <td><label for="firstName">First Name: </label></td>
                <td><g:textField name="firstName" id="firstName"/></td>
            </tr>
            <tr>
                <td><label for="lastName">Last Name: </label></td>
                <td><g:textField name="lastName" id="lastName"/></td>
            </tr>
            <tr>
                <td><label for="sex">Sex: </label></td>
                <td><g:radioGroup values="['M','F']" name="sex" labels="['Male','Female']">
                <p>${ it.radio } ${ it.label }</p>
                </g:radioGroup></td>
            </tr>
            <tr>
                <td><label for="email">Email: </label></td>
                <td><g:textField name="email" id="email"/></td>
            </tr>
            <tr>
                <td><label for="mobile">Mobile: </label></td>
                <td><g:textField name="mobile" id="mobile"/></td>
            </tr>
            <tr>
                <td><label for="passwordHash">Password: </label></td>
                <td><g:passwordField name="passwordHash" id="passwordHash"/></td>
            </tr>
            <tr>
                <td><label for="passwordHash2">Confirm password: </label></td>
                <td><g:passwordField name="passwordHash2" name="passwordHash2" id="passwordHash2" /></td>
            </tr>
            <tr>
                <td><label for="userType">User Type: </label></td>
                <td>
                    <g:radioGroup values="['F','H']" name="userType" labels="['Freelancer','Client']">
                        <p>${ it.radio } ${ it.label }</p>
                    </g:radioGroup>
                </td>
            </tr>
            <tr>
                <td><g:submitButton name="saveUser" value="Register"/> </td>
            </tr>
        </table>
        </g:form>
    </div>

</body>

maybe there’s something wrong in my code?

I want to just show the user an error, but not full exception trace

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T10:35:48+00:00Added an answer on June 6, 2026 at 10:35 am

    You are using save(failOnError:true) which will cause an exception to be thrown when your save fails. Since you are only catching the UserRegistrationException, the Exception from save() is producing a 500 Internal Server Error. You could remove the failOnError and your service method should fall through to your throw new UserRegistrationException block and behave as you expect.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code and can't figure out what seems to be the problem:
I have a bit of a problem that I can't seem to code my
I have such a basic problem in Delphi,I can't solve it. My Code: Note:DataR
I have a code problem which stems from the fact that I am using
I have a problem in this code when I enter a string, instead of
I have problem with javascript code. I am using google maps and I am
I have problem with this code: file = tempfile.TemporaryFile(mode='wrb') file.write(base64.b64decode(data)) file.flush() os.fsync(file) # file.seek(0)
Hi i have problem with this code, i found it on the internet and
i am a beginner and i have a problem : this code doesnt compile
I have a very strange problem and the following code wont compile: #ifndef MYWINDOW_HPP_INCLUDED

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.