SoFunction
Updated on 2025-03-04

Use custom error code to intercept grpc internal status code issues

Use custom error code to intercept grpc internal status code issues

Updated: September 18, 2023 10:45:19 Author: love666666shen
This article mainly introduces the problem of using custom error codes to intercept grpc internal status codes. It has good reference value. I hope it will be helpful to everyone. If there are any errors or no complete considerations, I hope you will be very encouraged.

Use custom error code to intercept grpc internal status code

In the golang project, when developing through grpc, when illegally passed parameters, parsing failures, and returning exceptions, although custom status codes are used, if no special processing is done, grpc will use its internal status code to intercept by default.

If you want to use a custom error code, what should you do?

Here we provide a way to use custom status codes freely. Although grpc status codes cannot be intercepted globally, it can also be processed concisely and conveniently. In detail, you only need to pass in the custom error code and error information description through () before returning to the error.

import (
	"/grpc/codes"
	"/grpc/status"
)
err = ((),  (int()))

Among them, resp represents the return structure, ErrNum represents the error code, and GetCodeDesc function returns the corresponding error information description through the error code.

The function definition is as follows:

// A Code is an unsigned 32-bit error code as defined in the gRPC spec.
type Code uint32
// Errorf returns Error(c, (format, a...)).
func Errorf(c , format string, a ...interface{}) error {
	return Error(c, (format, a...))
}

Grpc success and exception status code

Package:

package ;

Class:

Status 
    public static final Status OK;
    public static final Status CANCELLED;
    public static final Status UNKNOWN;
    public static final Status INVALID_ARGUMENT;
    public static final Status DEADLINE_EXCEEDED;
    public static final Status NOT_FOUND;
    public static final Status ALREADY_EXISTS;
    public static final Status PERMISSION_DENIED;
    public static final Status UNAUTHENTICATED;
    public static final Status RESOURCE_EXHAUSTED;
    public static final Status FAILED_PRECONDITION;
    public static final Status ABORTED;
    public static final Status OUT_OF_RANGE;
    public static final Status UNIMPLEMENTED;
    public static final Status INTERNAL;
    public static final Status UNAVAILABLE;
    public static final Status DATA_LOSS;
  • Ok: Return to success
  • Canceled: The operation has been cancelled
  • Unknown: Unknown error. An example of this error can be returned if the status value received from another address space belongs to an error space unknown in that address space. An error raised by an API that does not return enough error message may also be converted to this error
  • InvalidArgument: Indicates that the client has specified an invalid parameter. Note that this is different from FailedPrecondition. It indicates that there are problems regardless of system status (e.g., filenames with malformed format)
  • DeadlineExceeded: means that the operation expires before completion. This error may be returned even if the operation completes successfully for a system state. For example, a successful response from a server may delay sufficient time to make the deadline expire
  • NotFound: Indicates that the requested entity (such as a file or directory) cannot be found
  • AlreadyExists: Indicates that the attempt to create an entity failed because it already exists
  • PermissionDenied: means that the caller does not have permission to perform the specified operation. It cannot be used for rejections caused by exhausting certain resources (using ResourceExhausted instead of these errors). If the caller is not recognized, it cannot be used (Unauthenticated instead of these errors)
  • ResourceExhausted: Indicates that some resources have been exhausted, which may be quota per user, or the entire file system is insufficient
  • FailedPrecondition: Indicates that the operation is rejected because the system is not in the state required for the operation to be executed.
  • Aborted: means that the operation is aborted, usually caused by concurrency problems (such as sequencer check failure, transaction abnormal termination, etc.). See the litmus test above to determine the difference between FailedPrecondition, Aborted and Unavailable
  • OutOfRange: Indicates that the operation attempt exceeds the valid range.
  • Unimplemented: This method is not implemented
  • Internal: Meaning that some invariants expected by the underlying system have been broken. If you see one of these mistakes, things will be very bad
  • Unavailable: The internal Grpc service is not available and the request is not available
  • DataLoss: Indicates that unrecoverable data is lost or corrupted
  • Unauthenticated: Indicates that there is no valid operation authentication certificate for the request

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.

  • Custom errors
  • Code intercept
  • grpc
  • Status code

Related Articles

  • A brief discussion on the causes of Channel and deadlock in GO

    This article mainly introduces a brief discussion on the causes of channel and deadlock in GO. The article introduces the detailed description based on the example encoding in detail and has certain reference value. Interested friends can refer to it.
    2022-03-03
  • Detailed explanation of examples of building streaming data pipeline in Go language

    Go's concurrent primitives can easily build streaming data pipelines, thereby efficiently utilizing I/O and multiple CPUs. This article shows examples of such pipelines, emphasizes the subtleties that occur when the operation fails, and introduces techniques to deal with failures cleanly, hoping to be helpful to everyone.
    2024-02-02
  • Golang interface{} type conversion implementation example

    In Go language, type conversion can be implemented through four methods: assertion, explicit, implicit and coercion. For interface{} type conversion to float32 or float64, you need to use type assertion or reflect package processing. If you are interested, you can learn about it.
    2024-10-10
  • Implement pseudo-static URL rewrite function using Go

    In web development, pseudostatic URLs have become a common technical means to optimize website architecture and improve SEO. Pseudostatic URLs are a solution between dynamic URLs and static URLs. This article introduces you how to use Go to implement pseudostatic URL rewriting function. Friends who need it can refer to it.
    2024-08-08
  • Detailed explanation of why Golang channel does not block

    This article mainly introduces the reasons why Golang channel does not block. Friends in need can refer to it for reference. I hope it can be helpful. I wish you more progress and get a promotion as soon as possible.
    2022-07-07
  • How to implement mysql access and web api for Go project

    This article mainly introduces the implementation of mysql access and web api for Go projects. This article introduces you very detailed through the example code, which has certain reference value for your study or work. Friends who need it can refer to it.
    2023-08-08
  • Summary of the difference between make and new use in Golang

    In Go language, new and make are two built-in functions, mainly used to create allocated memory. This article mainly introduces the use and differences between functions new and make in Go language, which have certain reference value. If you are interested, you can learn about it.
    2024-01-01
  • Example code for Golang to draw rectangular boxes in an image

    This article mainly introduces the example code of Golang drawing rectangular boxes in an image. There are detailed code examples for your reference, which have certain reference value. Friends who need it can refer to it.
    2008-08-08
  • Golang uses Gin to create a Restful API implementation

    This article mainly introduces the implementation of Golang using Gin to create a Restful API. The example code is introduced in this article in detail, which has certain reference learning value for everyone's learning or work. If you need it, please learn with the editor below.
    2023-01-01
  • goland sets the console folding effect

    This article mainly introduces the folding effect of Goland setting console. This article introduces you very detailedly through pictures and texts, which has certain reference value for your study or work. Friends who need it can refer to it.
    2020-12-12

Latest Comments