alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Groovy example source code file (GroovyBugError.java)

This example Groovy source code file (GroovyBugError.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Groovy tags/keywords

assertionerror, bug, bug, exception, exception, groovybugerror, groovybugerror, string, string, throwable, uncaught

The Groovy GroovyBugError.java source code

/*
 * Copyright 2003-2007 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.codehaus.groovy;

/**
 * This class represents an error that is thrown when a bug is 
 * recognized inside the runtime. Basically it is thrown when
 * a constraint is not fullfilled that should be fullfiled. 
 * 
 * @author Jochen Theodorou
 */
public class GroovyBugError extends AssertionError {
    
    // message string
    private String message;
    // optional exception
    private final Exception exception;

    /**
     * constructs a bug error using the given text
     * @param message the error message text
     */
    public GroovyBugError( String message ) {
        this(message, null);
    }
    
    /**
     * Constructs a bug error using the given exception
     * @param exception cause of this error
     */
    public GroovyBugError( Exception exception ) {
        this(null, exception);
    }
    
    /**
     * Constructs a bug error using the given exception and
     * a text with additional information about the cause 
     * @param msg additional information about this error
     * @param exception cause of this error
     */
    public GroovyBugError( String msg, Exception exception ) {
        this.exception = exception;
        this.message = msg;
    }

    /**
     * Returns a String representation of this class by calling <code>getMessage().  
     * @see #getMessage()
     */
    public String toString() {
        return getMessage();
    }
    
    /**
     * Returns the detail message string of this error. The message 
     * will consist of the bug text prefixed by "BUG! " if there this
     * instance was created using a message. If this error was
     * constructed without using a bug text the message of the cause 
     * is used prefixed by "BUG! UNCAUGHT EXCEPTION: "
     *  
     * @return the detail message string of this error.
     */
    public String getMessage() {
        if( message != null )
        {
            return "BUG! "+message;
        }
        else
        {
            return "BUG! UNCAUGHT EXCEPTION: " + exception.getMessage();
        }
    }
    
    public Throwable getCause() {
        return this.exception;
    }    
    
    /**
     * Returns the bug text to describe this error
     */
    public String getBugText(){
        if( message != null ){
            return message;
        } else {
            return exception.getMessage();
        }
    }
    
    /**
     * Sets the bug text to describe this error
     */
    public void setBugText(String msg) {
        this.message = msg;
    }
}

Other Groovy examples (source code examples)

Here is a short list of links related to this Groovy GroovyBugError.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.