News

Failing Fast - identifying configuration problems early

by Hopton, Paul 10 March 2010 in MMS Software

When you deploy a piece of software it's easy to get something wrong. The server may not be set up to specification, or you may have a typo in your configuration. For simple applications these errors are easy to identify and standard test procedures should weed out the most obvious culprits. However more complex functionality is harder to test, and errors may be harder to identify. First I'll talk about a typical error, then I'll discuss a solution.

The problem - managing file uploads

A common feature of most of our software is user being able to upload files, images, data, documents for sharing. Over time we have developed a generic class for dealing with these UploadSpecification, this helps us to follow DRY principles. In the UploadSpecification we identify three characteristics common to file upload:

 

  1. uploadDir - the directory where the files will be stored
  2. uploadUrl - where the files will be accessed
  3. allowed  - which filetypes can be uploaded

The Interface for the class looks like this:

 

public interface UploadSpecifying {

    public abstract String getUploadDir();

    public abstract void setUploadDir(String uploadDir);

    public abstract String getUploadUrl();

    public abstract void setUploadUrl(String uploadUrl);

    public abstract List getAllowed();

    public abstract void setAllowed(List allowed);

}

 

Typically this will be configured in a Spring Configuration looking like this:

    <bean id="buildSpecificationTranslations" class="com.lessrain.projects.helpers.UploadSpecification">
        <list>
            <value>png</value>
            <value>jpg</value>
            <value>gif</value>
        </list>
        <property name="uploadDir" value="/var/www/userdata/images" />
        <property name="uploadUrl" value="http://localhost/images" />
    </bean>

If on the server there has been a misconfiguration with the location or privileges of the uploadDir, the UploadController using this specification will fail. On some projects we have over a dozen such specifications. If something is wrong we want to know about it at deployment time, not when a user tries to access it.

The Solution

In our implementation of UploadSpecifying I define the setUploadDir in the following way:

 

/* code simplified for readability*/
public void setUploadDir(String uploadDir) {
    
  File toCheck = new File(uploadDir);
    
  try{

    if ( ! toCheck.exists()){
      throw new FileNotFoundException(uploadDir + " does not exist");
    }
    if ( ! toCheck.isDirectory()){
      throw new IOException(uploadDir + " is not a directory");
    }
    if ( ! toCheck.canWrite()){
      throw new IOException(uploadDir + " is not writeable");
    }
  }
  catch(Exception e){
     throw new RuntimeException("unable to initialise UploadSpecfication", e);
  }

  this.uploadDir = uploadDir;

}

 

The setUploadDir method is called by Spring as it initialises the beans when starting up. If we have a misconfiguration in the uploadDir property, the Application will not startup and we should see a meaningful message in the logs. We can then quickly identify and correct the misconfiguration and restart the aplication.

This technique isn't particularly novel, but works well. It demonstrates nicely the advantages of code re-use and loose coupling as well as the good practice of failing fast.

Who We Are and What We Do

Meyer, Miller, Smith.

Head. Hands. Heart. We believe that there is a real connection between craftsmanship and communication.

As information and intelligence becomes the domain of computers, society will place more value on the one human ability that cannot be automated: Emotions.

People want to experience beauty, enjoy one’s work, feel passion, they want to interact with each other. We all want.

That’s why we believe that the future of brands is interaction, not commodity. It’s not something you buy, but something you participate in.

Top