UNPKG

1.78 kBPlain TextView Raw
1/*
2 * Copyright 2019 gRPC authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18import { StatusObject } from './call-stream';
19import { Status } from './constants';
20import { Metadata } from './metadata';
21
22/**
23 * A builder for gRPC status objects.
24 */
25export class StatusBuilder {
26 private code: Status | null;
27 private details: string | null;
28 private metadata: Metadata | null;
29
30 constructor() {
31 this.code = null;
32 this.details = null;
33 this.metadata = null;
34 }
35
36 /**
37 * Adds a status code to the builder.
38 */
39 withCode(code: Status): this {
40 this.code = code;
41 return this;
42 }
43
44 /**
45 * Adds details to the builder.
46 */
47 withDetails(details: string): this {
48 this.details = details;
49 return this;
50 }
51
52 /**
53 * Adds metadata to the builder.
54 */
55 withMetadata(metadata: Metadata): this {
56 this.metadata = metadata;
57 return this;
58 }
59
60 /**
61 * Builds the status object.
62 */
63 build(): Partial<StatusObject> {
64 const status: Partial<StatusObject> = {};
65
66 if (this.code !== null) {
67 status.code = this.code;
68 }
69
70 if (this.details !== null) {
71 status.details = this.details;
72 }
73
74 if (this.metadata !== null) {
75 status.metadata = this.metadata;
76 }
77
78 return status;
79 }
80}