...

Source file src/go.mongodb.org/mongo-driver/x/mongo/driver/mongocrypt/errors.go

Documentation: go.mongodb.org/mongo-driver/x/mongo/driver/mongocrypt

     1  // Copyright (C) MongoDB, Inc. 2017-present.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License"); you may
     4  // not use this file except in compliance with the License. You may obtain
     5  // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
     6  
     7  //go:build cse
     8  // +build cse
     9  
    10  package mongocrypt
    11  
    12  // #include <mongocrypt.h>
    13  import "C"
    14  import (
    15  	"fmt"
    16  )
    17  
    18  // Error represents an error from an operation on a MongoCrypt instance.
    19  type Error struct {
    20  	Code    int32
    21  	Message string
    22  }
    23  
    24  // Error implements the error interface.
    25  func (e Error) Error() string {
    26  	return fmt.Sprintf("mongocrypt error %d: %v", e.Code, e.Message)
    27  }
    28  
    29  // errorFromStatus builds a Error from a mongocrypt_status_t object.
    30  func errorFromStatus(status *C.mongocrypt_status_t) error {
    31  	cCode := C.mongocrypt_status_code(status) // uint32_t
    32  	// mongocrypt_status_message takes uint32_t* as its second param to store the length of the returned string.
    33  	// pass nil because the length is handled by C.GoString
    34  	cMsg := C.mongocrypt_status_message(status, nil) // const char*
    35  	var msg string
    36  	if cMsg != nil {
    37  		msg = C.GoString(cMsg)
    38  	}
    39  
    40  	return Error{
    41  		Code:    int32(cCode),
    42  		Message: msg,
    43  	}
    44  }
    45  

View as plain text