status-builder.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. import { StatusObject } from './call-stream';
  18. import { Status } from './constants';
  19. import { Metadata } from './metadata';
  20. /**
  21. * A builder for gRPC status objects.
  22. */
  23. export class StatusBuilder {
  24. private code: Status | null;
  25. private details: string | null;
  26. private metadata: Metadata | null;
  27. constructor() {
  28. this.code = null;
  29. this.details = null;
  30. this.metadata = null;
  31. }
  32. /**
  33. * Adds a status code to the builder.
  34. */
  35. withCode(code: Status): this {
  36. this.code = code;
  37. return this;
  38. }
  39. /**
  40. * Adds details to the builder.
  41. */
  42. withDetails(details: string): this {
  43. this.details = details;
  44. return this;
  45. }
  46. /**
  47. * Adds metadata to the builder.
  48. */
  49. withMetadata(metadata: Metadata): this {
  50. this.metadata = metadata;
  51. return this;
  52. }
  53. /**
  54. * Builds the status object.
  55. */
  56. build(): Partial<StatusObject> {
  57. const status: Partial<StatusObject> = {};
  58. if (this.code !== null) {
  59. status.code = this.code;
  60. }
  61. if (this.details !== null) {
  62. status.details = this.details;
  63. }
  64. if (this.metadata !== null) {
  65. status.metadata = this.metadata;
  66. }
  67. return status;
  68. }
  69. }