รหัสสถานะและการจัดการข้อผิดพลาด
เรียนรู้การใช้รหัสสถานะ gRPC อย่างมีประสิทธิภาพ และนำการส่งต่อรวมถึงการจัดการข้อผิดพลาดที่เหมาะสมมาใช้ในบริการของคุณ
รหัสสถานะและการจัดการข้อผิดพลาด เป็นบทเรียน gRPC & High Performance APIs ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน gRPC & High Performance APIs และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส gRPC & High Performance APIs มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Why Handle gRPC Errors?
In any robust application, errors are inevitable. How we handle them can make or break a system's reliability and user experience.
For distributed systems using gRPC, consistent error handling is crucial. It ensures that services can communicate problems clearly and clients can react appropriately.
Meet gRPC Status Codes
gRPC uses a standardized set of Status Codes to indicate the outcome of an RPC (Remote Procedure Call). These codes provide a universal way to understand why a call succeeded or failed.
Think of them like HTTP status codes, but specifically for gRPC. Some common ones include:
OK: The RPC completed successfully.NOT_FOUND: Resource not found (e.g., a user ID doesn't exist).INTERNAL: An unexpected error occurred on the server.UNAUTHENTICATED: The request lacks valid authentication credentials.
Protobuf Service Definition
Before we look at error handling, let's define a simple service in a .proto file. This defines the structure of our messages and the RPC methods.
We'll create a UserService with a GetUser method that takes a UserRequest and returns a User.
syntax = "proto3";
option java_multiple_files = true;
option java_package = "com.coddykit.grpc.error";
option java_outer_classname = "ErrorProto";
package errorhandling;
message UserRequest {
int32 id = 1;
}
message User {
int32 id = 1;
string name = 2;
string email = 3;
}
service UserService {
rpc GetUser (UserRequest) returns (User);
}Server-Side Error Signaling
On the server, when an operation fails, you don't throw a regular exception. Instead, you create a gRPC Status object with an appropriate code and description, then convert it to a StatusRuntimeException.
This exception is then sent back to the client via the responseObserver.onError() method, ensuring the client receives the standardized gRPC error.
Server Error Implementation
Try running this example. The server will respond with a NOT_FOUND error if you request any user ID other than 1.
import io.grpc.Server;
import io.grpc.ServerBuilder;
import io.grpc.Status;
import io.grpc.stub.StreamObserver;
import com.coddykit.grpc.error.ErrorProto.User;
import com.coddykit.grpc.error.ErrorProto.UserRequest;
import com.coddykit.grpc.error.UserServiceGrpc;
public class ErrorServer {
private static final int PORT = 50051;
public static void main(String[] args) throws Exception {
Server server = ServerBuilder.forPort(PORT)
.addService(new UserServiceImpl())
.build();
server.start();
System.out.println("Server started on port " + PORT);
server.awaitTermination();
}
static class UserServiceImpl extends UserServiceGrpc.UserServiceImplBase {
@Override
public void getUser(UserRequest request, StreamObserver<User> responseObserver) {
System.out.println("Received GetUser request for ID: " + request.getId());
if (request.getId() == 1) {
User user = User.newBuilder()
.setId(1)
.setName("Alice")
.setEmail("alice@example.com")
.build();
responseObserver.onNext(user);
responseObserver.onCompleted();
} else {
Status status = Status.NOT_FOUND.withDescription("User with ID " + request.getId() + " not found.");
responseObserver.onError(status.asRuntimeException());
}
}
}
}Client-Side Error Handling
On the client side, gRPC errors are typically received as StatusRuntimeException. You should wrap your gRPC calls in try-catch blocks to gracefully handle these exceptions.
Inside the catch block, you can inspect the Status object from the exception to determine the error code and description, allowing your client to respond intelligently.
Client Error Handling Demo
Run this client code after starting the server from the previous scene. Observe how it handles both a successful user lookup and a 'not found' error.
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.StatusRuntimeException;
import com.coddykit.grpc.error.ErrorProto.User;
import com.coddykit.grpc.error.ErrorProto.UserRequest;
import com.coddykit.grpc.error.UserServiceGrpc;
public class ErrorClient {
private static final int PORT = 50051;
private static final String HOST = "localhost";
public static void main(String[] args) {
ManagedChannel channel = ManagedChannelBuilder.forAddress(HOST, PORT)
.usePlaintext() // For local testing without TLS
.build();
UserServiceGrpc.UserServiceBlockingStub blockingStub = UserServiceGrpc.newBlockingStub(channel);
// Scenario 1: User found
try {
UserRequest foundRequest = UserRequest.newBuilder().setId(1).build();
User user = blockingStub.getUser(foundRequest);
System.out.println("User found: " + user.getName());
} catch (StatusRuntimeException e) {
System.err.println("Error calling GetUser (found scenario): " + e.getStatus().getCode() + " - " + e.getStatus().getDescription());
}
System.out.println("\n--- Trying to get a non-existent user ---");
// Scenario 2: User not found (expected error)
try {
UserRequest notFoundRequest = UserRequest.newBuilder().setId(99).build();
User user = blockingStub.getUser(notFoundRequest);
System.out.println("User found (unexpected): " + user.getName()); // This line should not be reached
} catch (StatusRuntimeException e) {
System.err.println("Error calling GetUser (not found scenario):");
System.err.println(" Status Code: " + e.getStatus().getCode());
System.err.println(" Description: " + e.getStatus().getDescription());
} finally {
channel.shutdown();
}
}
}Effective Error Propagation
Proper error propagation is vital. When a gRPC service calls another internal service and encounters an error, it's often best to:
- Log the error with sufficient detail for debugging.
- Translate the error into an appropriate gRPC
Statuscode for the calling client. Don't expose internal system errors directly. - Avoid swallowing errors. Always handle them or re-throw them so they don't disappear silently.
Test Your Knowledge
You are building a gRPC service that performs a complex calculation. If the input data is invalid (e.g., negative numbers where only positive are allowed), which gRPC Status code is most appropriate to return?
Key Takeaways
You've learned the basics of gRPC error handling!
- gRPC uses standardized Status Codes to communicate RPC outcomes.
- Servers signal errors by creating a
Statusobject and callingresponseObserver.onError(). - Clients handle errors by catching
StatusRuntimeExceptionand inspecting itsStatusobject. - Always propagate errors clearly and translate them appropriately for clients.
This structured approach ensures reliable communication in your distributed systems.
คำถามที่พบบ่อย
บทเรียน “รหัสสถานะและการจัดการข้อผิดพลาด” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “รหัสสถานะและการจัดการข้อผิดพลาด” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส gRPC & High Performance APIs ให้อัปเกรดเป็น CoddyKit PRO คอร์ส gRPC & High Performance APIs มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “รหัสสถานะและการจัดการข้อผิดพลาด”
เรียนรู้การใช้รหัสสถานะ gRPC อย่างมีประสิทธิภาพ และนำการส่งต่อรวมถึงการจัดการข้อผิดพลาดที่เหมาะสมมาใช้ในบริการของคุณ คุณปฏิบัติ gRPC & High Performance APIs ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน gRPC & High Performance APIs หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน gRPC & High Performance APIs บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “รหัสสถานะและการจัดการข้อผิดพลาด” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน gRPC & High Performance APIs นี้ได้ไหม
ได้ บทเรียน gRPC & High Performance APIs ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- รหัสสถานะและการจัดการข้อผิดพลาด
- การส่งข้อมูลเมตาแบบกำหนดเอง
- บริบทและกำหนดเวลา
- รูปแบบข้อผิดพลาดแบบละเอียดด้วย google.rpc.Status