Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.clustermate.client;
  
Container for details of a single failed call to a server.
  
  public class CallFailure
 {
     /*
     /**********************************************************************
     /* Internal information
     /**********************************************************************
      */
     
     protected final ServerNode _server;
 
     protected final long _callTime;
     
     protected final int _statusCode;
     
     protected final int _timeTakenMsecs;
 
     protected final Throwable _error;

    
Specific error message, either constructed explicitly, or extracted from JSON response.
 
     protected final String _errorMessage;

    
Excerpt of the underlying error message.
 
     protected final byte[] _rawResponse;
 
     /*
     /**********************************************************************
     /* Construction
     /**********************************************************************
      */
 
     public CallFailure(ServerNode serverint statusCode,
             long callTimelong endTime,
             String errorMsgbyte[] responseExcerpt)
     {
         this(serverstatusCodecallTimeendTimenullerrorMsgresponseExcerpt);
     }
     
     public CallFailure(ServerNode serverint statusCode,
             long callTimelong endTime,
             Throwable errorString errorMsgbyte[] responseExcerpt)
     {
          = server;
          = statusCode;
          = callTime;
          = (int) (endTime - callTime);
          = error;
          = errorMsg;
          = responseExcerpt;
     }
 
     public CallFailure(ServerNode serverint statusCode,
             long callTimelong endTime,
             Throwable errorbyte[] responseExcerpt)
     {
          = server;
          = statusCode;
          = callTime;
          = (int) (endTime - callTime);
          = error;
          = (error == null) ? "Unknown error" : error.toString();
          = responseExcerpt;
     }
    
    
Factory method called to indicate that a failure was due to timeout; either for actual timed out call, or not being able to make a call due to operation time out
 
     public static CallFailure timeout(ServerNode serverlong callTimelong endTime) {
                 callTimeendTime"timeout after "+ (endTime - callTime) + " msecs",
                 null);
     }

    
Factory method for general "not sure what or why failed" failure
 
     public static CallFailure general(ServerNode serverint statusCodelong callTime,
             long endTimeString msg) {
         return new CallFailure(serverstatusCodecallTimeendTimemsgnull);
     }

    
Factory method for "could not parse response" failure.
 
     public static CallFailure formatException(ServerNode serverint statusCodelong callTime,
            long endTimeString msg) {
        return new CallFailure(serverstatusCodecallTimeendTimemsgnull);
    }
    
    
Factory method for general I/O failure, caused by HTTP request or response processing.
    public static CallFailure ioProblem(ServerNode serverint statusCodelong callTime,
            long endTimeString msgException e)
    {
        if (e != null) {
            Throwable cause = ExceptionUtil.peel(e);
            msg += " (exception of type "+cause.getClass().getName()+": "+cause.getMessage()+")";
        }
        return new CallFailure(serverstatusCodecallTimeendTimemsgnull);
    }
    
    
Factory method for "internal failure"; case where something in our code threw an exception causing individual call to fail, and we have an exception indicating what happened.
    public static CallFailure clientInternal(ServerNode serverlong callTime,
            long endTimeThrowable cause)
    {
        cause = ExceptionUtil.peel(cause);
        
        // Let's produce stack trace for certain problems
        if (cause instanceof NullPointerException) {
                    callTimeendTimecause,
                    "NullPointerException at "+ExceptionUtil.getStackTraceDesc(cause, 5),
                    null);            
        }
        
                callTimeendTimecausenull);
    }

    
Factory method for "internal failure"; case where something in our code threw an exception causing individual call to fail, but we did not get an exception.
    public static CallFailure clientInternal(ServerNode serverlong callTime,
            long endTimeString msg) {
                callTimeendTimemsgnull);
    }
    
    /*
    /**********************************************************************
    /* Simple accessors
    /**********************************************************************
     */
    
    public ServerNode getServer() { return ; }
    public int getStatusCode() { return ; }

    
Accessor that can be used to find out client-side java.lang.Throwable that caused call to fail, if failure was due to a java.lang.Throwable being thrown.
    public Throwable getCause() { return ; }
    
    
Accessor for getting timestamp of when request attempt was made ("start time")
    public long getCallTime() { return ; }
    public long getEndTime() {
        if ( > 0) {
            return  + ;
        }
        return ;
    }
    
    public int getTimeTakenMsecs() { return ; }

    
Accessor for explicitly provided failure message; either from code that knows what failed, or from response message successfully parsed.
    public String getErrorMessage() {
        return ( == null) ? "N/A" : ;
    }

    
Accessor for getting actual underlying error message (or, if very long, extract of first N bytes). Null for failures that did not get a response.
    public String getResponseExtract()
    {
        if ( == null) {
            return null;
        }
        if (. == 0) {
            return "";
        }
        try {
            return new String("ISO-8859-1");
        } catch (Exception e) {
            return "DECODING ERROR for "+.+" bytes: "+e.getMessage();
        }
    }

    
Accessor that can be called to determine whether failure is of transient type, and might be resolved by retrying after a brief delay.
    public boolean isRetriable()
    {
        // We can actually just retry 5xx codes (which includes timeouts)
        return ( >= 500) && ( < 600);
    }
    
    /*
    /**********************************************************************
    /* Overrides
    /**********************************************************************
     */
    
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder(100);
        sb.append("[Call Failure to ").append(.getAddress())
            .append(": status=").append()
            .append(", errorMessage='").append().append("'");
        String resp = getResponseExtract();
        if (resp != null) {
            sb.append(", response='").append(resp).append("'");
        }
        sb.append(']');
        return sb.toString();
    }
New to GrepCode? Check out our FAQ X