EMMA Coverage Report (generated Mon Oct 17 05:41:20 EDT 2011)
[all classes][org.jclouds.gogrid.handlers]

COVERAGE SUMMARY FOR SOURCE FILE [GoGridErrorHandler.java]

nameclass, %method, %block, %line, %
GoGridErrorHandler.java100% (1/1)100% (3/3)54%  (43/80)64%  (13.5/21)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class GoGridErrorHandler100% (1/1)100% (3/3)54%  (43/80)64%  (13.5/21)
handleError (HttpCommand, HttpResponse): void 100% (1/1)48%  (29/61)66%  (8.5/13)
parseErrorsFromContentOrNull (HttpResponse): Set 100% (1/1)62%  (8/13)40%  (2/5)
GoGridErrorHandler (ParseErrorFromJsonResponse): void 100% (1/1)100% (6/6)100% (3/3)

1/**
2 * Licensed to jclouds, Inc. (jclouds) under one or more
3 * contributor license agreements.  See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership.  jclouds licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License.  You may obtain a copy of the License at
9 *
10 *   http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied.  See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19package org.jclouds.gogrid.handlers;
20 
21import static org.jclouds.http.HttpUtils.releasePayload;
22 
23import java.util.Set;
24 
25import org.jclouds.gogrid.GoGridResponseException;
26import org.jclouds.gogrid.domain.internal.ErrorResponse;
27import org.jclouds.gogrid.functions.ParseErrorFromJsonResponse;
28import org.jclouds.http.HttpCommand;
29import org.jclouds.http.HttpErrorHandler;
30import org.jclouds.http.HttpResponse;
31import org.jclouds.http.HttpResponseException;
32import org.jclouds.rest.AuthorizationException;
33import org.jclouds.rest.ResourceNotFoundException;
34 
35import com.google.common.collect.Iterables;
36import com.google.inject.Inject;
37 
38/**
39 * @author Oleksiy Yarmula
40 */
41public class GoGridErrorHandler implements HttpErrorHandler {
42 
43   private final ParseErrorFromJsonResponse errorParser;
44 
45   @Inject
46   public GoGridErrorHandler(ParseErrorFromJsonResponse errorParser) {
47      this.errorParser = errorParser;
48   }
49 
50   @Override
51   public void handleError(HttpCommand command, HttpResponse response) {
52      try {
53         Exception exception = new HttpResponseException(command, response);
54         Set<ErrorResponse> errors = parseErrorsFromContentOrNull(response);
55         if (errors != null)
56            exception = new GoGridResponseException(command, response, errors);
57         switch (response.getStatusCode()) {
58         case 400:
59            if (Iterables.get(errors, 0).getMessage().indexOf("No object found") != -1) {
60               exception = new ResourceNotFoundException(Iterables.get(errors, 0).getMessage(), exception);
61               break;
62            }
63         case 403:
64            exception = new AuthorizationException(exception.getMessage(), exception);
65            break;
66         }
67         command.setException(exception);
68      } finally {
69         releasePayload(response);
70      }
71   }
72 
73   Set<ErrorResponse> parseErrorsFromContentOrNull(HttpResponse response) {
74      if (response.getPayload() != null) {
75         try {
76            return errorParser.apply(response);
77         } catch (/* Parsing */Exception e) {
78            return null;
79         }
80      }
81      return null;
82   }
83}

[all classes][org.jclouds.gogrid.handlers]
EMMA 2.0.5312 (C) Vladimir Roubtsov