@matter/protocol 0.15.2-alpha.0-20250717-3607eeac6 → 0.15.2-alpha.0-20250719-e4aeb457c

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. package/dist/cjs/action/errors.d.ts +7 -7
  2. package/dist/cjs/action/errors.d.ts.map +1 -1
  3. package/dist/cjs/action/errors.js +9 -14
  4. package/dist/cjs/action/errors.js.map +1 -1
  5. package/dist/cjs/action/response/WriteResult.d.ts +4 -3
  6. package/dist/cjs/action/response/WriteResult.d.ts.map +1 -1
  7. package/dist/cjs/action/response/WriteResult.js +30 -0
  8. package/dist/cjs/action/response/WriteResult.js.map +2 -2
  9. package/dist/cjs/common/ExpandedPath.d.ts +25 -0
  10. package/dist/cjs/common/ExpandedPath.d.ts.map +1 -0
  11. package/dist/cjs/common/ExpandedPath.js +73 -0
  12. package/dist/cjs/common/ExpandedPath.js.map +6 -0
  13. package/dist/cjs/common/ExpandedStatus.d.ts +27 -0
  14. package/dist/cjs/common/ExpandedStatus.d.ts.map +1 -0
  15. package/dist/cjs/common/ExpandedStatus.js +73 -0
  16. package/dist/cjs/common/ExpandedStatus.js.map +6 -0
  17. package/dist/cjs/common/PathError.d.ts +28 -0
  18. package/dist/cjs/common/PathError.d.ts.map +1 -0
  19. package/dist/cjs/common/PathError.js +52 -0
  20. package/dist/cjs/common/PathError.js.map +6 -0
  21. package/dist/cjs/common/index.d.ts +1 -0
  22. package/dist/cjs/common/index.d.ts.map +1 -1
  23. package/dist/cjs/common/index.js +1 -0
  24. package/dist/cjs/common/index.js.map +1 -1
  25. package/dist/esm/action/errors.d.ts +7 -7
  26. package/dist/esm/action/errors.d.ts.map +1 -1
  27. package/dist/esm/action/errors.js +10 -15
  28. package/dist/esm/action/errors.js.map +1 -1
  29. package/dist/esm/action/response/WriteResult.d.ts +4 -3
  30. package/dist/esm/action/response/WriteResult.d.ts.map +1 -1
  31. package/dist/esm/action/response/WriteResult.js +26 -0
  32. package/dist/esm/action/response/WriteResult.js.map +2 -2
  33. package/dist/esm/common/ExpandedPath.d.ts +25 -0
  34. package/dist/esm/common/ExpandedPath.d.ts.map +1 -0
  35. package/dist/esm/common/ExpandedPath.js +60 -0
  36. package/dist/esm/common/ExpandedPath.js.map +6 -0
  37. package/dist/esm/common/ExpandedStatus.d.ts +27 -0
  38. package/dist/esm/common/ExpandedStatus.d.ts.map +1 -0
  39. package/dist/esm/common/ExpandedStatus.js +53 -0
  40. package/dist/esm/common/ExpandedStatus.js.map +6 -0
  41. package/dist/esm/common/PathError.d.ts +28 -0
  42. package/dist/esm/common/PathError.d.ts.map +1 -0
  43. package/dist/esm/common/PathError.js +32 -0
  44. package/dist/esm/common/PathError.js.map +6 -0
  45. package/dist/esm/common/index.d.ts +1 -0
  46. package/dist/esm/common/index.d.ts.map +1 -1
  47. package/dist/esm/common/index.js +1 -0
  48. package/dist/esm/common/index.js.map +1 -1
  49. package/package.json +6 -6
  50. package/src/action/errors.ts +16 -21
  51. package/src/action/response/WriteResult.ts +27 -3
  52. package/src/common/ExpandedPath.ts +87 -0
  53. package/src/common/ExpandedStatus.ts +71 -0
  54. package/src/common/PathError.ts +46 -0
  55. package/src/common/index.ts +1 -0
@@ -5,9 +5,13 @@
5
5
  */
6
6
 
7
7
  import { Write } from "#action/request/Write.js";
8
- import type { AttributeId, AttributePath, ClusterId, EndpointNumber, NodeId, StatusCode } from "#types";
8
+ import { ExpandedPath } from "#common/ExpandedPath.js";
9
+ import { ExpandedStatus } from "#common/ExpandedStatus.js";
10
+ import { PathError } from "#common/PathError.js";
11
+ import { MatterAggregateError } from "#general";
12
+ import { Status, type AttributeId, type AttributePath, type ClusterId, type EndpointNumber, type NodeId } from "#types";
9
13
 
10
- export type WriteResult<T extends Write> = Promise<
14
+ export type WriteResult<T extends Write = Write> = Promise<
11
15
  T extends { suppressResponse: true } ? void : WriteResult.AttributeStatus[]
12
16
  >;
13
17
 
@@ -23,7 +27,27 @@ export namespace WriteResult {
23
27
  export interface AttributeStatus {
24
28
  kind: "attr-status";
25
29
  path: ConcreteAttributePath;
26
- status: StatusCode;
30
+ status: Status;
27
31
  clusterStatus?: number;
28
32
  }
33
+
34
+ export function assertSuccess(result: AttributeStatus[]) {
35
+ const errors = result
36
+ .filter(attr => attr.status !== Status.Success)
37
+ .map(attr => {
38
+ const path = ExpandedPath({ ...attr, kind: "attribute" });
39
+ const status = new ExpandedStatus(attr);
40
+ return new PathError({ path, status });
41
+ });
42
+
43
+ if (!errors.length) {
44
+ return;
45
+ }
46
+
47
+ if (errors.length === 1) {
48
+ throw errors[0];
49
+ }
50
+
51
+ throw new MatterAggregateError(errors, "Multiple writes failed");
52
+ }
29
53
  }
@@ -0,0 +1,87 @@
1
+ /**
2
+ * @license
3
+ * Copyright 2022-2025 Matter.js Authors
4
+ * SPDX-License-Identifier: Apache-2.0
5
+ */
6
+
7
+ import { camelize } from "#general";
8
+ import {
9
+ AttributeModel,
10
+ ClusterModel,
11
+ CommandModel,
12
+ DataModelPath,
13
+ EventModel,
14
+ Matter,
15
+ MatterModel,
16
+ Model,
17
+ } from "#model";
18
+ import { AttributePath, CommandPath, EventPath } from "#types";
19
+
20
+ /**
21
+ * Creates a {@link DataModelPath} from a Matter wire-format {@link AttributePath}, {@link EventPath}, or
22
+ * {@link CommandPath}.
23
+ *
24
+ * This is useful for creating human-readable diagnostics.
25
+ */
26
+ export function ExpandedPath({ path, matter, base, kind }: ExpandedPath.Definition): DataModelPath {
27
+ if (matter === undefined) {
28
+ matter = Matter;
29
+ }
30
+
31
+ if (base && "path" in base) {
32
+ base = base.path;
33
+ }
34
+
35
+ const endpointIdent = path.endpointId ?? "*";
36
+ base = base ? base.at(endpointIdent, "endpoint") : DataModelPath(endpointIdent, "endpoint");
37
+
38
+ let cluster: ClusterModel | undefined;
39
+ base = base.at(identityOf(matter, ClusterModel, path.clusterId), "cluster");
40
+
41
+ if ("attributeId" in path) {
42
+ base = base.at("state").at(identityOf(cluster, AttributeModel, path.attributeId));
43
+ if (path.listIndex) {
44
+ return base.at(path.listIndex, "entry");
45
+ }
46
+ return base;
47
+ }
48
+
49
+ if ("commandId" in path) {
50
+ return base.at(identityOf(cluster, CommandModel, path.commandId));
51
+ }
52
+
53
+ if ("eventId" in path) {
54
+ return base.at("events").at(identityOf(cluster, EventModel, path.eventId));
55
+ }
56
+
57
+ return base.at("*", kind ?? "element");
58
+
59
+ function identityOf(parent: undefined | Model, type: Model.Type, id: undefined | number | string) {
60
+ if (id === undefined) {
61
+ return "*";
62
+ }
63
+
64
+ const instance = parent?.get(type, id);
65
+ if (instance === undefined) {
66
+ if (typeof id === "string") {
67
+ return camelize(id);
68
+ }
69
+ return id;
70
+ }
71
+
72
+ if (type === ClusterModel) {
73
+ cluster = instance as ClusterModel;
74
+ }
75
+
76
+ return camelize(instance.name);
77
+ }
78
+ }
79
+
80
+ export namespace ExpandedPath {
81
+ export interface Definition {
82
+ path: AttributePath | EventPath | CommandPath;
83
+ matter?: MatterModel;
84
+ base?: DataModelPath | { path: DataModelPath };
85
+ kind?: "attribute" | "command" | "event" | "entry";
86
+ }
87
+ }
@@ -0,0 +1,71 @@
1
+ /**
2
+ * @license
3
+ * Copyright 2022-2025 Matter.js Authors
4
+ * SPDX-License-Identifier: Apache-2.0
5
+ */
6
+
7
+ import { decamelize } from "#general";
8
+ import { ClusterModel, Matter, MatterModel } from "#model";
9
+ import { ClusterId, Status } from "#types";
10
+
11
+ /**
12
+ * Detailed status information expanded from status codes.
13
+ *
14
+ * TODO - maybe update codegen to include description in operational models so we can use as detailed error message
15
+ */
16
+ export class ExpandedStatus {
17
+ id: string;
18
+ status: Status;
19
+ clusterStatus?: number;
20
+
21
+ constructor({ matter, status, cluster, clusterStatus }: ExpandedStatus.Definition) {
22
+ this.status = status ?? Status.Failure;
23
+ this.clusterStatus = clusterStatus;
24
+
25
+ let statusStr: undefined | string;
26
+ if (status !== Status.Failure) {
27
+ const name = Status[this.status];
28
+ if (name === undefined) {
29
+ statusStr = `unknown-${this.status}`;
30
+ } else {
31
+ statusStr = decamelize(name);
32
+ }
33
+ }
34
+
35
+ if (clusterStatus === undefined) {
36
+ this.id = statusStr ?? "failure";
37
+ return;
38
+ }
39
+
40
+ let clusterStr;
41
+ if (typeof cluster === "number") {
42
+ matter ??= Matter;
43
+ const model = matter.get(ClusterModel, cluster);
44
+ if (model === undefined) {
45
+ clusterStr = `cluster-${cluster}`;
46
+ } else {
47
+ clusterStr = decamelize(model.name);
48
+ }
49
+ } else if (cluster) {
50
+ clusterStr = decamelize(cluster.name);
51
+ } else {
52
+ clusterStr = "unknown-cluster";
53
+ }
54
+
55
+ if (statusStr === undefined) {
56
+ this.id = clusterStr;
57
+ return;
58
+ }
59
+
60
+ this.id = `${statusStr}+${clusterStr}`;
61
+ }
62
+ }
63
+
64
+ export namespace ExpandedStatus {
65
+ export interface Definition {
66
+ matter?: MatterModel;
67
+ status?: Status;
68
+ cluster?: ClusterModel | ClusterId;
69
+ clusterStatus?: number;
70
+ }
71
+ }
@@ -0,0 +1,46 @@
1
+ /**
2
+ * @license
3
+ * Copyright 2022-2025 Matter.js Authors
4
+ * SPDX-License-Identifier: Apache-2.0
5
+ */
6
+
7
+ import { ExpandedStatus } from "#common/ExpandedStatus.js";
8
+ import { camelize, capitalize, decamelize, MatterAggregateError } from "#general";
9
+ import { DataModelPath } from "#model";
10
+ import { StatusResponseError } from "#types";
11
+
12
+ /**
13
+ * A protocol error associated with a specific data model path.
14
+ */
15
+ export class PathError extends StatusResponseError {
16
+ #id: string;
17
+ #path: DataModelPath;
18
+
19
+ constructor({ path, status: { id, status, clusterStatus }, message }: PathError.Definition) {
20
+ if (message === undefined) {
21
+ message = capitalize(decamelize(camelize(id), " "));
22
+ }
23
+
24
+ super(message, status, clusterStatus);
25
+ this.#id = id;
26
+ this.#path = path;
27
+ }
28
+
29
+ override get id() {
30
+ return this.#id;
31
+ }
32
+
33
+ get path() {
34
+ return this.#path;
35
+ }
36
+ }
37
+
38
+ export namespace PathError {
39
+ export interface Definition {
40
+ path: DataModelPath;
41
+ status: ExpandedStatus;
42
+ message?: string;
43
+ }
44
+ }
45
+
46
+ export class AggregatePathError extends MatterAggregateError {}
@@ -5,6 +5,7 @@
5
5
  */
6
6
 
7
7
  export * from "../peer/PeerAddress.js";
8
+ export * from "./ExpandedPath.js";
8
9
  export * from "./FailsafeContext.js";
9
10
  export * from "./FailsafeTimer.js";
10
11
  export * from "./InstanceBroadcaster.js";