@oondemand/oon-core-back 0.4.25 → 0.4.26

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oondemand/oon-core-back",
3
- "version": "0.4.25",
3
+ "version": "0.4.26",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/oondemand/oon-platform.git",
@@ -125,14 +125,50 @@ function translateProviderActivationError(error, fallback) {
125
125
 
126
126
  const timedOut = error.code === "ECONNABORTED";
127
127
  const statusCode = timedOut ? 504 : error.response?.status || 502;
128
+ const payload = error.response?.data;
129
+ const envelope = payload?.error && typeof payload.error === "object"
130
+ ? payload.error
131
+ : payload;
132
+ const responseHeaders = error.response?.headers;
133
+ const headerRequestId = typeof responseHeaders?.get === "function"
134
+ ? responseHeaders.get("x-request-id") || responseHeaders.get("x-correlation-id")
135
+ : responseHeaders?.["x-request-id"] || responseHeaders?.["x-correlation-id"];
136
+ const requestId = String(
137
+ envelope?.requestId
138
+ || envelope?.details?.requestId
139
+ || headerRequestId
140
+ || "",
141
+ ).trim();
142
+ const upstreamDetails = envelope?.details;
143
+ const details = upstreamDetails && typeof upstreamDetails === "object"
144
+ ? { ...upstreamDetails, ...(requestId ? { requestId } : {}) }
145
+ : {
146
+ ...(upstreamDetails !== undefined ? { upstream: upstreamDetails } : {}),
147
+ ...(requestId ? { requestId } : {}),
148
+ };
128
149
  const message =
129
- error.response?.data?.message ||
130
- error.response?.data?.error?.message ||
131
- (timedOut
150
+ envelope?.message
151
+ || payload?.message
152
+ || (timedOut
132
153
  ? "A Central de Ativações não respondeu dentro do prazo."
133
154
  : fallback);
134
155
 
135
- return new GenericError(message, { statusCode });
156
+ return new GenericError(message, {
157
+ statusCode,
158
+ code: envelope?.code || (timedOut ? "ACTIVATION_PROVIDER_TIMEOUT" : "ACTIVATION_PROVIDER_FAILED"),
159
+ details: Object.keys(details).length ? details : null,
160
+ });
161
+ }
162
+
163
+ async function activationProviderPost(...args) {
164
+ try {
165
+ return await providerPost(...args);
166
+ } catch (error) {
167
+ throw translateProviderActivationError(
168
+ error,
169
+ "A Central de Ativações recusou a operação.",
170
+ );
171
+ }
136
172
  }
137
173
 
138
174
  function idOf(value) {
@@ -522,7 +558,7 @@ router.post(
522
558
  });
523
559
 
524
560
  try {
525
- const response = await providerPost(
561
+ const response = await activationProviderPost(
526
562
  req,
527
563
  PATHS.validar,
528
564
  providerPayload(req, codigoAtivacao),
@@ -601,14 +637,14 @@ router.post(
601
637
  });
602
638
  }
603
639
 
604
- const preview = await providerPost(
640
+ const preview = await activationProviderPost(
605
641
  req,
606
642
  PATHS.validar,
607
643
  providerPayload(req, codigoAtivacao),
608
644
  );
609
645
  assertApp(preview.data);
610
646
 
611
- const activated = await providerPost(
647
+ const activated = await activationProviderPost(
612
648
  req,
613
649
  PATHS.ativar,
614
650
  providerPayload(req, codigoAtivacao, { nomeInstancia, metadata: {} }),
@@ -682,7 +718,7 @@ async function complete(req, instance, config) {
682
718
  });
683
719
  }
684
720
 
685
- await providerPost(
721
+ await activationProviderPost(
686
722
  req,
687
723
  PATHS.concluir,
688
724
  {
@@ -746,3 +782,4 @@ router.post(
746
782
  );
747
783
 
748
784
  module.exports = router;
785
+ module.exports.translateProviderActivationError = translateProviderActivationError;