aboutsummaryrefslogtreecommitdiff
path: root/src/com/google/caliper/UserException.java
blob: 66fb8e35b85fbb9eb94557d607d8066a522e2228 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
 * Copyright (C) 2009 Google Inc.
 *
 * 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 com.google.caliper;

import java.util.Arrays;

/**
 * Signifies a problem that should be explained in user-friendly terms on the command line, without
 * a confusing stack trace, and optionally followed by a usage summary.
 */
@SuppressWarnings("serial") // never going to serialize these... right?
public abstract class UserException extends RuntimeException {
  protected final String error;

  protected UserException(String error) {
    this.error = error;
  }

  public abstract void display();

  // - - - -

  public abstract static class ErrorInUsageException extends UserException {
    protected ErrorInUsageException(String error) {
      super(error);
    }

    @Override public void display() {
      if (error != null) {
        System.err.println("Error: " + error);
      }
      Arguments.printUsage();
    }
  }

  public abstract static class ErrorInUserCodeException extends UserException {
    private final String remedy;

    protected ErrorInUserCodeException(String error, String remedy) {
      super(error);
      this.remedy = remedy;
    }

    @Override public void display() {
      System.err.println("Error: " + error);
      System.err.println("Typical Remedy: " + remedy);
    }
  }

  // - - - -

  // Not technically an error, but works nicely this way anyway
  public static class DisplayUsageException extends ErrorInUsageException {
    public DisplayUsageException() {
      super(null);
    }
  }

  public static class UnrecognizedOptionException extends ErrorInUsageException {
    public UnrecognizedOptionException(String arg) {
      super("Argument not recognized: " + arg);
    }
  }

  public static class NoBenchmarkClassException extends ErrorInUsageException {
    public NoBenchmarkClassException() {
      super("No benchmark class specified.");
    }
  }

  public static class MultipleBenchmarkClassesException extends ErrorInUsageException {
    public MultipleBenchmarkClassesException(String a, String b) {
      super("Multiple benchmark classes specified: " + Arrays.asList(a, b));
    }
  }

  public static class MalformedParameterException extends ErrorInUsageException {
    public MalformedParameterException(String arg) {
      super("Malformed parameter: " + arg);
    }
  }

  public static class CantCustomizeInProcessVmException extends ErrorInUsageException {
    public CantCustomizeInProcessVmException() {
      super("Can't customize VM when running in process.");
    }
  }

  public static class NoSuchClassException extends ErrorInUsageException {
    public NoSuchClassException(String name) {
      super("No class named [" + name + "] was found (check CLASSPATH).");
    }
  }


  public static class AbstractBenchmarkException extends ErrorInUserCodeException {
    public AbstractBenchmarkException(Class<?> specifiedClass) {
      super("Class [" + specifiedClass.getName() + "] is abstract.", "Specify a concrete class.");
    }
  }

  public static class NoParameterlessConstructorException extends ErrorInUserCodeException {
    public NoParameterlessConstructorException(Class<?> specifiedClass) {
      super("Class [" + specifiedClass.getName() + "] has no parameterless constructor.",
          "Remove all constructors or add a parameterless constructor.");
    }
  }

  public static class DoesntImplementBenchmarkException extends ErrorInUserCodeException {
    public DoesntImplementBenchmarkException(Class<?> specifiedClass) {
      super("Class [" + specifiedClass + "] does not implement the " + Benchmark.class.getName()
          + " interface.", "Add 'extends " + SimpleBenchmark.class + "' to the class declaration.");
    }
  }

  // TODO: should remove the caliper stack frames....
  public static class ExceptionFromUserCodeException extends UserException {
    public ExceptionFromUserCodeException(Throwable t) {
      super("An exception was thrown from the benchmark code.");
      initCause(t);
    }
    @Override public void display() {
      System.err.println(error);
      getCause().printStackTrace(System.err);
    }
  }
}