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

     1  package ravendb
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  )
     7  
     8  type CancellationError struct {
     9  }
    10  
    11  func (e *CancellationError) Error() string {
    12  	return "CancellationError"
    13  }
    14  
    15  type errorBase struct {
    16  	wrapped  error
    17  	ErrorStr string
    18  }
    19  
    20  // Error makes it conform to error interface
    21  func (e *errorBase) Error() string {
    22  	return e.ErrorStr
    23  }
    24  
    25  // hackish way to get a wrapped error
    26  func (e *errorBase) WrappedError() error {
    27  	return e.wrapped
    28  }
    29  
    30  type iWrappedError interface {
    31  	WrappedError() error
    32  }
    33  
    34  // GetWrappedError returns an error wrapped by this error
    35  // If no error is wrapped, returns nil
    36  func GetWrappedError(err error) error {
    37  	if e, ok := err.(iWrappedError); ok {
    38  		return e.WrappedError()
    39  	}
    40  	return nil
    41  }
    42  
    43  func (e *errorBase) setErrorf(format string, args ...interface{}) {
    44  	if len(args) == 0 {
    45  		e.ErrorStr = format
    46  		return
    47  	}
    48  	// a bit of a hack: to make it easy to port Java code, if the last
    49  	// argument is of type error, we consider it a wrapped error
    50  	n := len(args)
    51  	last := args[n-1]
    52  	if err, ok := last.(error); ok {
    53  		e.wrapped = err
    54  		args = args[:n-1]
    55  	}
    56  	e.ErrorStr = fmt.Sprintf(format, args...)
    57  }
    58  
    59  // RuntimeError represents generic runtime error
    60  type RuntimeError struct {
    61  	errorBase
    62  }
    63  
    64  func newRuntimeError(format string, args ...interface{}) *RuntimeError {
    65  	res := &RuntimeError{}
    66  	res.setErrorf(format, args...)
    67  	return res
    68  }
    69  
    70  // UnsupportedOperationError represents unsupported operation error
    71  type UnsupportedOperationError struct {
    72  	errorBase
    73  }
    74  
    75  func newUnsupportedOperationError(format string, args ...interface{}) *UnsupportedOperationError {
    76  	res := &UnsupportedOperationError{}
    77  	res.setErrorf(format, args...)
    78  	return res
    79  }
    80  
    81  // IllegalStateError represents illegal state error
    82  type IllegalStateError struct {
    83  	errorBase
    84  }
    85  
    86  func newIllegalStateError(format string, args ...interface{}) *IllegalStateError {
    87  	res := &IllegalStateError{}
    88  	res.setErrorf(format, args...)
    89  	return res
    90  }
    91  
    92  // IllegalArgumentError represents illegal argument error
    93  type IllegalArgumentError struct {
    94  	errorBase
    95  }
    96  
    97  func newIllegalArgumentError(format string, args ...interface{}) *IllegalArgumentError {
    98  	res := &IllegalArgumentError{}
    99  	res.setErrorf(format, args...)
   100  	return res
   101  }
   102  
   103  // NotImplementedError represents not implemented error
   104  type NotImplementedError struct {
   105  	errorBase
   106  }
   107  
   108  func newNotImplementedError(format string, args ...interface{}) *NotImplementedError {
   109  	res := &NotImplementedError{}
   110  	res.setErrorf(format, args...)
   111  	return res
   112  }
   113  
   114  // AllTopologyNodesDownError represents "all topology nodes are down" error
   115  type AllTopologyNodesDownError struct {
   116  	errorBase
   117  }
   118  
   119  func newAllTopologyNodesDownError(format string, args ...interface{}) *AllTopologyNodesDownError {
   120  	res := &AllTopologyNodesDownError{}
   121  	res.setErrorf(format, args...)
   122  	return res
   123  }
   124  
   125  // OperationCancelledError represents "operation cancelled" error
   126  type OperationCancelledError struct {
   127  	errorBase
   128  }
   129  
   130  func newOperationCancelledError(format string, args ...interface{}) *OperationCancelledError {
   131  	res := &OperationCancelledError{}
   132  	res.setErrorf(format, args...)
   133  	return res
   134  }
   135  
   136  // AuthorizationError represents authorization error
   137  type AuthorizationError struct {
   138  	errorBase
   139  }
   140  
   141  func newAuthorizationError(format string, args ...interface{}) *AuthorizationError {
   142  	res := &AuthorizationError{}
   143  	res.setErrorf(format, args...)
   144  	return res
   145  }
   146  
   147  // RavenError represents generic raven error
   148  // all exceptions that in Java extend RavenException should
   149  // contain this error
   150  type RavenError struct {
   151  	errorBase
   152  }
   153  
   154  // hackish way to see if "inherits" from (embeds) RavenError
   155  func (e *RavenError) isRavenError() bool {
   156  	return true
   157  }
   158  
   159  type iRavenError interface {
   160  	isRavenError() bool
   161  }
   162  
   163  func isRavenError(err error) bool {
   164  	_, ok := err.(iRavenError)
   165  	return ok
   166  }
   167  
   168  func newRavenError(format string, args ...interface{}) *RavenError {
   169  	res := &RavenError{}
   170  	res.setErrorf(format, args...)
   171  	return res
   172  }
   173  
   174  // ConcurrencyError represents concurrency error
   175  type ConcurrencyError struct {
   176  	RavenError
   177  
   178  	ExpectedETag         int64
   179  	ActualETag           int64
   180  	ExpectedChangeVector string
   181  	ActualChangeVector   string
   182  }
   183  
   184  func newConcurrencyError(format string, args ...interface{}) *ConcurrencyError {
   185  	res := &ConcurrencyError{}
   186  	res.setErrorf(format, args...)
   187  	return res
   188  }
   189  
   190  // NonUniqueObjectError represents non unique object error
   191  type NonUniqueObjectError struct {
   192  	RavenError
   193  }
   194  
   195  // newNonUniqueObjectError creates new NonUniqueObjectError
   196  func newNonUniqueObjectError(format string, args ...interface{}) *NonUniqueObjectError {
   197  	res := &NonUniqueObjectError{}
   198  	res.setErrorf(format, args...)
   199  	return res
   200  }
   201  
   202  // DatabaseDoesNotExistError represents "database not not exist" error
   203  type DatabaseDoesNotExistError struct {
   204  	RavenError
   205  }
   206  
   207  // newDatabaseDoesNotExistError creates new NonUniqueObjectError
   208  func newDatabaseDoesNotExistError(format string, args ...interface{}) *DatabaseDoesNotExistError {
   209  	res := &DatabaseDoesNotExistError{}
   210  	res.setErrorf(format, args...)
   211  	return res
   212  }
   213  
   214  // TimeoutError represents timeout error
   215  type TimeoutError struct {
   216  	RavenError
   217  }
   218  
   219  // NewTimeoutError returns new TimeoutError
   220  func NewTimeoutError(format string, args ...interface{}) *TimeoutError {
   221  	res := &TimeoutError{}
   222  	res.setErrorf(format, args...)
   223  	return res
   224  }
   225  
   226  // IndexDoesNotExistError represents "index doesn't exist" error
   227  type IndexDoesNotExistError struct {
   228  	RavenError
   229  }
   230  
   231  func newIndexDoesNotExistError(format string, args ...interface{}) *IndexDoesNotExistError {
   232  	res := &IndexDoesNotExistError{}
   233  	res.setErrorf(format, args...)
   234  	return res
   235  }
   236  
   237  // BadResponseError represents "bad response" error
   238  type BadResponseError struct {
   239  	RavenError
   240  }
   241  
   242  func newBadResponseError(format string, args ...interface{}) *BadResponseError {
   243  	res := &BadResponseError{}
   244  	res.setErrorf(format, args...)
   245  	return res
   246  }
   247  
   248  // BadRequestError maps to server's 400 Bad Request response
   249  // This is additional information sent by the server
   250  type BadRequestError struct {
   251  	RavenError
   252  }
   253  
   254  // ConflictError maps to server's 409 Conflict response
   255  type ConflictError struct {
   256  	RavenError
   257  }
   258  
   259  func newConflictError(format string, args ...interface{}) *ConflictError {
   260  	res := &ConflictError{}
   261  	res.setErrorf(format, args...)
   262  	return res
   263  }
   264  
   265  // a base type for subscription-related errors
   266  type SubscriptionError struct {
   267  	RavenError
   268  }
   269  
   270  // SubscriberErrorError represents error about subscriber error
   271  // Note: name is unfortunate but it corresponds to Java's SubscriberErrorException
   272  type SubscriberErrorError struct {
   273  	SubscriptionError
   274  }
   275  
   276  // SubscriptionChangeVectorUpdateConcurrencyError represents an error about
   277  // subscription change vector update concurrency
   278  type SubscriptionChangeVectorUpdateConcurrencyError struct {
   279  	SubscriptionError
   280  }
   281  
   282  func newSubscriptionChangeVectorUpdateConcurrencyError(format string, args ...interface{}) *SubscriptionChangeVectorUpdateConcurrencyError {
   283  	res := &SubscriptionChangeVectorUpdateConcurrencyError{}
   284  	res.setErrorf(format, args...)
   285  	return res
   286  }
   287  
   288  // SubscriptionClosedError is returned when subscription is closed
   289  type SubscriptionClosedError struct {
   290  	SubscriptionError
   291  }
   292  
   293  func newSubscriptionClosedError(format string, args ...interface{}) *SubscriptionClosedError {
   294  	res := &SubscriptionClosedError{}
   295  	res.setErrorf(format, args...)
   296  	return res
   297  }
   298  
   299  // SubscriptionDoesNotBelongToNodeError is returned when subscription
   300  // does not belong to node
   301  type SubscriptionDoesNotBelongToNodeError struct {
   302  	SubscriptionError
   303  
   304  	appropriateNode string
   305  }
   306  
   307  func newSubscriptionDoesNotBelongToNodeError(format string, args ...interface{}) *SubscriptionDoesNotBelongToNodeError {
   308  	res := &SubscriptionDoesNotBelongToNodeError{}
   309  	res.setErrorf(format, args...)
   310  	return res
   311  }
   312  
   313  // SubscriptionDoesNotExistError is returned when subscription doesn't exist
   314  type SubscriptionDoesNotExistError struct {
   315  	SubscriptionError
   316  }
   317  
   318  func newSubscriptionDoesNotExistError(format string, args ...interface{}) *SubscriptionDoesNotExistError {
   319  	res := &SubscriptionDoesNotExistError{}
   320  	res.setErrorf(format, args...)
   321  	return res
   322  }
   323  
   324  // SubscriptionInvalidStateError is returned when subscription is in invalid state
   325  type SubscriptionInvalidStateError struct {
   326  	SubscriptionError
   327  }
   328  
   329  func newSubscriptionInvalidStateError(format string, args ...interface{}) *SubscriptionInvalidStateError {
   330  	res := &SubscriptionInvalidStateError{}
   331  	res.setErrorf(format, args...)
   332  	return res
   333  }
   334  
   335  // SubscriptionInUseError is returned when subscription is in use
   336  type SubscriptionInUseError struct {
   337  	SubscriptionError
   338  }
   339  
   340  func newSubscriptionInUseError(format string, args ...interface{}) *SubscriptionInUseError {
   341  	res := &SubscriptionInUseError{}
   342  	res.setErrorf(format, args...)
   343  	return res
   344  }
   345  
   346  // ClientVersionMismatchError is returned when subscription is in use
   347  type ClientVersionMismatchError struct {
   348  	RavenError
   349  }
   350  
   351  // CertificateNameMismatchError is returned when subscription is in use
   352  type CertificateNameMismatchError struct {
   353  	errorBase
   354  }
   355  
   356  func throwCancellationRequested() error {
   357  	return newOperationCancelledError("")
   358  }
   359  
   360  type InvalidQueryError struct {
   361  	RavenError
   362  }
   363  
   364  type UnsuccessfulRequestError struct {
   365  	RavenError
   366  }
   367  
   368  type ChangeProcessingError struct {
   369  	RavenError
   370  }
   371  
   372  type CommandExecutionError struct {
   373  	RavenError
   374  }
   375  
   376  type NodeIsPassiveError struct {
   377  	RavenError
   378  }
   379  
   380  type NoLeaderError struct {
   381  	RavenError
   382  }
   383  
   384  type LicenseActivationError struct {
   385  	RavenError
   386  }
   387  
   388  type CompilationError struct {
   389  	RavenError
   390  }
   391  
   392  type DatabaseConcurrentLoadTimeoutError struct {
   393  	RavenError
   394  }
   395  
   396  type DatabaseDisabledError struct {
   397  	RavenError
   398  }
   399  
   400  type DatabaseLoadFailureError struct {
   401  	RavenError
   402  }
   403  
   404  type DatabaseLoadTimeoutError struct {
   405  	RavenError
   406  }
   407  
   408  type DatabaseNotRelevantError struct {
   409  	RavenError
   410  }
   411  
   412  type DocumentDoesNotExistError struct {
   413  	RavenError
   414  }
   415  
   416  type BulkInsertProtocolViolationError struct {
   417  	RavenError
   418  }
   419  
   420  type IndexAlreadyExistError struct {
   421  	RavenError
   422  }
   423  
   424  type IndexCreationError struct {
   425  	RavenError
   426  }
   427  
   428  type IndexDeletionError struct {
   429  	RavenError
   430  }
   431  
   432  type IndexInvalidError struct {
   433  	RavenError
   434  }
   435  
   436  type JavaScriptError struct {
   437  	RavenError
   438  }
   439  
   440  type RevisionsDisabledError struct {
   441  	RavenError
   442  }
   443  
   444  type RouteNotFoundError struct {
   445  	RavenError
   446  }
   447  
   448  type SecurityError struct {
   449  	RavenError
   450  }
   451  
   452  type ServerLoadFailureError struct {
   453  	RavenError
   454  }
   455  
   456  type IndexCompilationError struct {
   457  	RavenError
   458  }
   459  
   460  func makeRavenErrorFromName(exceptionName string, errMsg string) error {
   461  	// Java's "FooException" is "FooError" in Go
   462  	s := strings.Replace(exceptionName, "Exception", "Error", -1)
   463  	switch s {
   464  	case "IndexCompilationError":
   465  		res := &IndexCompilationError{}
   466  		res.ErrorStr = errMsg
   467  		return res
   468  	case "ConcurrencyError":
   469  		res := &ConcurrencyError{}
   470  		res.ErrorStr = errMsg
   471  		return res
   472  	case "NonUniqueObjectError":
   473  		res := &NonUniqueObjectError{}
   474  		res.ErrorStr = errMsg
   475  		return res
   476  	case "DatabaseDoesNotExistError":
   477  		res := &DatabaseDoesNotExistError{}
   478  		res.ErrorStr = errMsg
   479  		return res
   480  	case "TimeoutError":
   481  		res := &TimeoutError{}
   482  		res.ErrorStr = errMsg
   483  		return res
   484  	case "IndexDoesNotExistError":
   485  		res := &IndexDoesNotExistError{}
   486  		res.ErrorStr = errMsg
   487  		return res
   488  	case "BadResponseError":
   489  		res := &BadResponseError{}
   490  		res.ErrorStr = errMsg
   491  		return res
   492  	case "BadRequestError":
   493  		res := &BadRequestError{}
   494  		res.ErrorStr = errMsg
   495  		return res
   496  	case "ConflictError":
   497  		res := &ConflictError{}
   498  		res.ErrorStr = errMsg
   499  		return res
   500  	case "SubscriberErrorError":
   501  		res := &SubscriberErrorError{}
   502  		res.ErrorStr = errMsg
   503  		return res
   504  	case "SubscriptionChangeVectorUpdateConcurrencyError":
   505  		res := &SubscriptionChangeVectorUpdateConcurrencyError{}
   506  		res.ErrorStr = errMsg
   507  		return res
   508  	case "SubscriptionClosedError":
   509  		res := &SubscriptionClosedError{}
   510  		res.ErrorStr = errMsg
   511  		return res
   512  	case "SubscriptionDoesNotBelongToNodeError":
   513  		res := &SubscriptionDoesNotBelongToNodeError{}
   514  		res.ErrorStr = errMsg
   515  		return res
   516  	case "SubscriptionDoesNotExistError":
   517  		res := &SubscriptionDoesNotExistError{}
   518  		res.ErrorStr = errMsg
   519  		return res
   520  	case "SubscriptionInvalidStateError":
   521  		res := &SubscriptionInvalidStateError{}
   522  		res.ErrorStr = errMsg
   523  		return res
   524  	case "SubscriptionInUseError":
   525  		res := &SubscriptionInUseError{}
   526  		res.ErrorStr = errMsg
   527  		return res
   528  	case "ClientVersionMismatchError":
   529  		res := &ClientVersionMismatchError{}
   530  		res.ErrorStr = errMsg
   531  		return res
   532  	case "CertificateNameMismatchError":
   533  		res := &CertificateNameMismatchError{}
   534  		res.ErrorStr = errMsg
   535  		return res
   536  	case "InvalidQueryError":
   537  		res := &InvalidQueryError{}
   538  		res.ErrorStr = errMsg
   539  		return res
   540  	case "UnsuccessfulRequestError":
   541  		res := &UnsuccessfulRequestError{}
   542  		res.ErrorStr = errMsg
   543  		return res
   544  	case "ChangeProcessingError":
   545  		res := &ChangeProcessingError{}
   546  		res.ErrorStr = errMsg
   547  		return res
   548  	case "CommandExecutionError":
   549  		res := &CommandExecutionError{}
   550  		res.ErrorStr = errMsg
   551  		return res
   552  	case "NodeIsPassiveError":
   553  		res := &NodeIsPassiveError{}
   554  		res.ErrorStr = errMsg
   555  		return res
   556  	case "NoLeaderError":
   557  		res := &NoLeaderError{}
   558  		res.ErrorStr = errMsg
   559  		return res
   560  	case "LicenseActivationError":
   561  		res := &LicenseActivationError{}
   562  		res.ErrorStr = errMsg
   563  		return res
   564  	case "CompilationError":
   565  		res := &CompilationError{}
   566  		res.ErrorStr = errMsg
   567  		return res
   568  	case "DatabaseConcurrentLoadTimeoutError":
   569  		res := &DatabaseConcurrentLoadTimeoutError{}
   570  		res.ErrorStr = errMsg
   571  		return res
   572  	case "DatabaseDisabledError":
   573  		res := &DatabaseDisabledError{}
   574  		res.ErrorStr = errMsg
   575  		return res
   576  	case "DatabaseLoadFailureError":
   577  		res := &DatabaseLoadFailureError{}
   578  		res.ErrorStr = errMsg
   579  		return res
   580  	case "DatabaseLoadTimeoutError":
   581  		res := &DatabaseLoadTimeoutError{}
   582  		res.ErrorStr = errMsg
   583  		return res
   584  	case "DatabaseNotRelevantError":
   585  		res := &DatabaseNotRelevantError{}
   586  		res.ErrorStr = errMsg
   587  		return res
   588  	case "DocumentDoesNotExistError":
   589  		res := &DocumentDoesNotExistError{}
   590  		res.ErrorStr = errMsg
   591  		return res
   592  	case "BulkInsertAbortedError":
   593  		res := &BulkInsertAbortedError{}
   594  		res.ErrorStr = errMsg
   595  		return res
   596  	case "BulkInsertProtocolViolationError":
   597  		res := &BulkInsertProtocolViolationError{}
   598  		res.ErrorStr = errMsg
   599  		return res
   600  	case "IndexAlreadyExistError":
   601  		res := &IndexAlreadyExistError{}
   602  		res.ErrorStr = errMsg
   603  		return res
   604  	case "IndexCreationError":
   605  		res := &IndexCreationError{}
   606  		res.ErrorStr = errMsg
   607  		return res
   608  	case "IndexDeletionError":
   609  		res := &IndexDeletionError{}
   610  		res.ErrorStr = errMsg
   611  		return res
   612  	case "IndexInvalidError":
   613  		res := &IndexInvalidError{}
   614  		res.ErrorStr = errMsg
   615  		return res
   616  	case "JavaScriptError":
   617  		res := &JavaScriptError{}
   618  		res.ErrorStr = errMsg
   619  		return res
   620  	case "RevisionsDisabledError":
   621  		res := &RevisionsDisabledError{}
   622  		res.ErrorStr = errMsg
   623  		return res
   624  	case "RouteNotFoundError":
   625  		res := &RouteNotFoundError{}
   626  		res.ErrorStr = errMsg
   627  		return res
   628  	case "SecurityError":
   629  		res := &SecurityError{}
   630  		res.ErrorStr = errMsg
   631  		return res
   632  	case "ServerLoadFailureError":
   633  		res := &ServerLoadFailureError{}
   634  		res.ErrorStr = errMsg
   635  		return res
   636  
   637  	}
   638  	return nil
   639  }