func UnaryServerInterceptor(db *gorm.DB) grpc.UnaryServerInterceptor {
	return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
		// prepare new *Transaction instance
		txn := &Transaction{parent: db}

		defer func() {
			// simple panic handler
			if perr := recover(); perr != nil {
				// we do not try to safe the world -
				// just attempt to close our transaction
				// re-raise panic and let someone to handle it
				txn.Rollback()
				panic(perr)
			}

			var terr error
			if err != nil {
				terr = txn.Rollback()
			} else {
				if terr = txn.Commit(ctx); terr != nil {
					err = status.Error(codes.Internal, "failed to commit transaction")
				}
			}

			if terr == nil {
				return
			}

			st := status.Convert(err)
			st, serr := st.WithDetails(errdetails.New(codes.Internal, "gorm", terr.Error()))
			// do not override error if failed to attach details
			if serr == nil {
				err = st.Err()
			}
			return
		}()

		ctx = NewContext(ctx, txn)
		resp, err = handler(ctx, req)

		return resp, err
	}
}
// UnaryServerInterceptor returns grpc.UnaryServerInterceptor that manages
// a `*Transaction` instance.
// New *Transaction instance is created before grpc.UnaryHandler call.
// Client is responsible to call `txn.Begin()` to open transaction.
// If call of grpc.UnaryHandler returns with an error the transaction
// is aborted, otherwise committed.
UnaryServerInterceptor returns grpc.UnaryServerInterceptor that manages
a `*Transaction` instance.
New *Transaction instance is created before grpc.UnaryHandler call.
Client is responsible to call `txn.Begin()` to open transaction.
If call of grpc.UnaryHandler returns with an error the transaction
is aborted, otherwise committed.
// UnaryServerInterceptor returns grpc . UnaryServerInterceptor that manages // a ` * Transaction ` instance . // New * Transaction instance is created before grpc . UnaryHandler call . // Client is responsible to call ` txn . Begin () ` to open transaction . // If call of grpc . UnaryHandler returns with an error the transaction // is aborted otherwise committed .
func UnaryServerInterceptor(db *gorm.DB) grpc.UnaryServerInterceptor {
	return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
		txn := &Transaction{parent: db}
		defer func() {
			if perr := recover(); perr != nil {
				txn.Rollback()
				panic(perr)
			}
			var terr error
			if err != nil {
				terr = txn.Rollback()
			} else {
				if terr = txn.Commit(ctx); terr != nil {
					err = status.Error(codes.Internal, "failed to commit transaction")
				}
			}
			if terr == nil {
				return
			}
			st := status.Convert(err)
			st, serr := st.WithDetails(errdetails.New(codes.Internal, "gorm", terr.Error()))
			if serr == nil {
				err = st.Err()
			}
			return
		}()
		ctx = NewContext(ctx, txn)
		resp, err = handler(ctx, req)
		return resp, err
	}
}
func UnaryServerInterceptor ( db * gorm . DB ) grpc . UnaryServerInterceptor { return func ( ctx context . Context , req interface { } , info * grpc . UnaryServerInfo , handler grpc . UnaryHandler ) ( resp interface { } , err error ) { txn := & Transaction { parent : db } defer func ( ) { if perr := recover ( ) ; perr != nil { txn . Rollback ( ) panic ( perr ) } var terr error if err != nil { terr = txn . Rollback ( ) } else { if terr = txn . Commit ( ctx ) ; terr != nil { err = status . Error ( codes . Internal , " " ) } } if terr == nil { return } st := status . Convert ( err ) st , serr := st . WithDetails ( errdetails . New ( codes . Internal , " " , terr . Error ( ) ) ) if serr == nil { err = st . Err ( ) } return } ( ) ctx = NewContext ( ctx , txn ) resp , err = handler ( ctx , req ) return resp , err } }
UnaryServerInterceptor returns grpc.UnaryServerInterceptor that manages
a `*Transaction` instance.
UnaryServerInterceptor returns grpc . UnaryServerInterceptor that manages a ` * Transaction ` instance .
// prepare new *Transaction instance // simple panic handler // we do not try to safe the world - // just attempt to close our transaction // re-raise panic and let someone to handle it // do not override error if failed to attach details