Skip to content

Commit

Permalink
add rpc error message
Browse files Browse the repository at this point in the history
  • Loading branch information
MikePresman committed Dec 13, 2023
1 parent f61ed37 commit e2a7143
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 13 deletions.
15 changes: 15 additions & 0 deletions js/spec/grpc-client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import { decodeContent, encodeContent } from "../src";
import { grpcClient } from "./util";

describe("grpc client operations", () => {
afterEach(async () => {
await grpcClient.deleteProject(1337n);
});

it("can create and read an object", async () => {
await grpcClient.newProject(1337n, []);
const content = encodeContent("a v1");
Expand Down Expand Up @@ -102,4 +106,15 @@ describe("grpc client operations", () => {

expect(result).toBe(12n);
});

it("throws a proper error when recreating the same project", async () => {
await grpcClient.newProject(1337n, []);

try {
await grpcClient.newProject(1337n, []);
expect(true).toBe(false);
} catch (error) {
expect((error as Error).message).toBe("project id 1337 already exists");
}
});
});
32 changes: 20 additions & 12 deletions js/src/grpc-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { ChannelCredentials, credentials, Metadata } from "@grpc/grpc-js";
import type { Span } from "@opentelemetry/api";
import { context as contextAPI, trace as traceAPI } from "@opentelemetry/api";
import { GrpcTransport } from "@protobuf-ts/grpc-transport";
import type { ClientStreamingCall, RpcOptions } from "@protobuf-ts/runtime-rpc";
import { RpcError, type ClientStreamingCall, type RpcOptions } from "@protobuf-ts/runtime-rpc";
import { TextDecoder, TextEncoder } from "util";
import { trace, tracer } from "./internal/telemetry";
import type { CloneToProjectResponse, GetUnaryResponse, Objekt, Project, UpdateRequest, UpdateResponse } from "./pb/fs_pb";
import { FsClient } from "./pb/fs_pb.client";

import { ProjectAlreadyExistsError } from "./utils/errors";
export type { Objekt, Project };

/**
Expand Down Expand Up @@ -119,17 +119,25 @@ export class DateiLagerGrpcClient {
* @param template The id of the project to start from.
*/
public async newProject(project: bigint, packPatterns: string[], template?: bigint): Promise<void> {
await trace(
"dateilager-grpc-client.new-project",
{
attributes: {
"dl.project": String(project),
"dl.pack_patterns": packPatterns,
"dl.template": String(template),
try {
await trace(
"dateilager-grpc-client.new-project",
{
attributes: {
"dl.project": String(project),
"dl.pack_patterns": packPatterns,
"dl.template": String(template),
},
},
},
() => this._client.newProject({ id: project, packPatterns, template }, this._rpcOptions())
);
() => this._client.newProject({ id: project, packPatterns, template }, this._rpcOptions())
);
} catch (error) {
if (error instanceof RpcError && error.code == "ALREADY_EXISTS") {
throw new ProjectAlreadyExistsError(`project id ${project} already exists`);
} else {
throw error;
}
}
}

/**
Expand Down
5 changes: 5 additions & 0 deletions js/src/utils/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export class ProjectAlreadyExistsError extends Error {
constructor(msg: string) {
super(msg);
}
}
6 changes: 5 additions & 1 deletion pkg/api/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,11 @@ func (f *Fs) NewProject(ctx context.Context, req *pb.NewProjectRequest) (*pb.New

err = db.CreateProject(ctx, tx, req.Id, req.PackPatterns)
if err != nil {
return nil, status.Errorf(codes.Internal, "FS new project %v, %v", req.Id, err)
rpcErrorCode := codes.Internal
if err.Error() == "project id already exists" {
rpcErrorCode = codes.AlreadyExists
}
return nil, status.Errorf(rpcErrorCode, "FS new project %v, %v", req.Id, err)
}

if req.Template != nil {
Expand Down

0 comments on commit e2a7143

Please sign in to comment.