github.com/altipla-consulting/ravendb-go-client@v0.1.3/exception_dispatcher.go (about)

     1  package ravendb
     2  
     3  import (
     4  	"io/ioutil"
     5  	"net/http"
     6  	"strings"
     7  )
     8  
     9  func exceptionDispatcherGetFromSchema(schema *exceptionSchema, code int, inner error) error {
    10  	return exceptionDispatcherGet(schema.Message, schema.Error, schema.Type, code, inner)
    11  }
    12  
    13  func exceptionDispatcherGet(message string, errStr string, typeAsString string, code int, inner error) error {
    14  	if code == http.StatusConflict {
    15  		if strings.Contains(typeAsString, "DocumentConflictException") {
    16  			return newDocumentConflictErrorFromMessage(message)
    17  		}
    18  		return newConcurrencyError(message)
    19  	}
    20  	err := exceptionDispatherMakeErrorFromType(typeAsString, errStr)
    21  	if err == nil {
    22  		return newRavenError("%s", errStr, inner)
    23  	}
    24  
    25  	return err
    26  }
    27  
    28  func exceptionDispatcherThrowError(response *http.Response) error {
    29  	if response == nil {
    30  		return newIllegalArgumentError("Response cannot be null")
    31  	}
    32  	var d []byte
    33  	var err error
    34  	if response.Body != nil {
    35  		d, err = ioutil.ReadAll(response.Body)
    36  		_ = response.Body.Close()
    37  		if err != nil {
    38  			return newRavenError("%s", err.Error(), err)
    39  		}
    40  	}
    41  	var schema exceptionSchema
    42  	if len(d) > 0 {
    43  		err = jsonUnmarshal(d, &schema)
    44  		if err != nil {
    45  			return newRavenError("%")
    46  		}
    47  		if response.StatusCode == http.StatusConflict {
    48  			return exceptionDispatcherThrowConflict(&schema, string(d))
    49  		}
    50  	}
    51  
    52  	exception := exceptionDispatherMakeErrorFromType(schema.Type, schema.Error)
    53  	if exception == nil {
    54  		return newRavenError("%s. Response: %s", schema.Error, string(d), exception)
    55  	}
    56  
    57  	// TODO: handle IndexCompilationError
    58  	/*
    59  	   if (IndexCompilationException.class.equals(type)) {
    60  	       IndexCompilationException indexCompilationException = (IndexCompilationException) exception;
    61  	       JsonNode jsonNode = JsonExtensions.getDefaultMapper().readTree(json);
    62  	       JsonNode indexDefinitionProperty = jsonNode.get("TransformerDefinitionProperty");
    63  	       if (indexDefinitionProperty != null) {
    64  	           indexCompilationException.setIndexDefinitionProperty(indexDefinitionProperty.asText());
    65  	       }
    66  
    67  	       JsonNode problematicText = jsonNode.get("ProblematicText");
    68  	       if (problematicText != null) {
    69  	           indexCompilationException.setProblematicText(problematicText.asText());
    70  	       }
    71  
    72  	       throw indexCompilationException;
    73  	   }
    74  
    75  	*/
    76  
    77  	return exception
    78  }
    79  
    80  func exceptionDispatcherThrowConflict(schema *exceptionSchema, js string) error {
    81  	if strings.Contains(schema.Type, "DocumentConflictException") {
    82  		return newDocumentConflictErrorFromJSON(js)
    83  	}
    84  	return newConcurrencyError("%s", schema.Message)
    85  }
    86  
    87  // make an error corresponding to C#'s exception name as returned by the server
    88  func exceptionDispatherMakeErrorFromType(typeAsString string, errMsg string) error {
    89  	if typeAsString == "System.TimeoutException" {
    90  		return &TimeoutError{}
    91  	}
    92  
    93  	exceptionName := strings.TrimPrefix(typeAsString, "Raven.Client.Exceptions.")
    94  	if exceptionName == typeAsString {
    95  		return nil
    96  	}
    97  	// those could be further namespaced, take only the last part
    98  	parts := strings.Split(exceptionName, ".")
    99  	if len(parts) > 1 {
   100  		exceptionName = parts[len(parts)-1]
   101  	}
   102  	return makeRavenErrorFromName(exceptionName, errMsg)
   103  }
   104  
   105  type exceptionSchema struct {
   106  	URL     string `json:"Url"`
   107  	Type    string `json:"Type"`
   108  	Message string `json:"Message"`
   109  	Error   string `json:"Error"`
   110  }