envio 2.27.6 → 2.28.0-alpha.1

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.
@@ -44,8 +44,14 @@ module Dict = {
44
44
  }
45
45
  }
46
46
 
47
- @inline
48
- let panic = message => Exn.raiseError(Exn.makeError(`[rescript-rest] ${message}`))
47
+ let panic = (message, ~params: option<{..}>=?) => {
48
+ let error = Exn.makeError(`[rescript-rest] ${message}`)
49
+ switch params {
50
+ | Some(params) => (error->Obj.magic)["params"] = params
51
+ | None => ()
52
+ }
53
+ Exn.raiseError(error)
54
+ }
49
55
 
50
56
  @val
51
57
  external encodeURIComponent: string => string = "encodeURIComponent"
@@ -808,6 +814,9 @@ let fetch = (type input response, route: route<input, response>, input, ~client=
808
814
  | S.Raised(error) =>
809
815
  panic(
810
816
  `Failed parsing response at ${error.path->S.Path.toString}. Reason: ${error->S.Error.reason}`,
817
+ ~params={
818
+ "response": fetcherResponse,
819
+ },
811
820
  )
812
821
  }
813
822
  }
@@ -1,9 +1,18 @@
1
1
  // Generated by ReScript, PLEASE EDIT WITH CARE
2
2
  'use strict';
3
3
 
4
+ var Caml_option = require("rescript/lib/js/caml_option.js");
4
5
  var S$RescriptSchema = require("rescript-schema/src/S.res.js");
5
6
  var Caml_js_exceptions = require("rescript/lib/js/caml_js_exceptions.js");
6
7
 
8
+ function panic(message, params) {
9
+ var error = new Error("[rescript-rest] " + message);
10
+ if (params !== undefined) {
11
+ error.params = Caml_option.valFromOption(params);
12
+ }
13
+ throw error;
14
+ }
15
+
7
16
  async function $$default(args) {
8
17
  var result = await fetch(args.path, args);
9
18
  var contentType = result.headers.get("content-type");
@@ -34,9 +43,11 @@ var ApiFetcher = {
34
43
 
35
44
  function register(map, status, builder) {
36
45
  if (map[status]) {
37
- throw new Error("[rescript-rest] " + ("Response for the \"" + status + "\" status registered multiple times"));
46
+ return panic("Response for the \"" + status + "\" status registered multiple times", undefined);
47
+ } else {
48
+ map[status] = builder;
49
+ return ;
38
50
  }
39
- map[status] = builder;
40
51
  }
41
52
 
42
53
  function parsePath(_path, pathItems, pathParams) {
@@ -48,24 +59,24 @@ function parsePath(_path, pathItems, pathParams) {
48
59
  var paramStartIdx = path.indexOf("{");
49
60
  if (paramStartIdx !== -1) {
50
61
  var paramEndIdx = path.indexOf("}");
51
- if (paramEndIdx !== -1) {
52
- if (paramStartIdx > paramEndIdx) {
53
- throw new Error("[rescript-rest] Path parameter is not enclosed in curly braces");
54
- }
55
- var paramName = path.slice(paramStartIdx + 1 | 0, paramEndIdx);
56
- if (paramName === "") {
57
- throw new Error("[rescript-rest] Path parameter name cannot be empty");
58
- }
59
- var param = {
60
- name: paramName
61
- };
62
- pathItems.push(path.slice(0, paramStartIdx));
63
- pathItems.push(param);
64
- pathParams[paramName] = param;
65
- _path = path.slice(paramEndIdx + 1 | 0);
66
- continue ;
62
+ if (paramEndIdx === -1) {
63
+ return panic("Path contains an unclosed parameter", undefined);
64
+ }
65
+ if (paramStartIdx > paramEndIdx) {
66
+ panic("Path parameter is not enclosed in curly braces", undefined);
67
67
  }
68
- throw new Error("[rescript-rest] Path contains an unclosed parameter");
68
+ var paramName = path.slice(paramStartIdx + 1 | 0, paramEndIdx);
69
+ if (paramName === "") {
70
+ panic("Path parameter name cannot be empty", undefined);
71
+ }
72
+ var param = {
73
+ name: paramName
74
+ };
75
+ pathItems.push(path.slice(0, paramStartIdx));
76
+ pathItems.push(param);
77
+ pathParams[paramName] = param;
78
+ _path = path.slice(paramEndIdx + 1 | 0);
79
+ continue ;
69
80
  }
70
81
  pathItems.push(path);
71
82
  return ;
@@ -279,7 +290,7 @@ function params(route) {
279
290
  match.TAG === "literal" && match._0.kind === "String" ? false : true
280
291
  );
281
292
  if (isNonStringBased) {
282
- throw new Error("[rescript-rest] Only string-based schemas are allowed in rawBody");
293
+ panic("Only string-based schemas are allowed in rawBody", undefined);
283
294
  }
284
295
  ((isRawBody = true));
285
296
  return s.f("body", schema);
@@ -292,7 +303,7 @@ function params(route) {
292
303
  }),
293
304
  param: (function (fieldName, schema) {
294
305
  if (!pathParams[fieldName]) {
295
- throw new Error("[rescript-rest] " + ("Path parameter \"" + fieldName + "\" is not defined in the path"));
306
+ panic("Path parameter \"" + fieldName + "\" is not defined in the path", undefined);
296
307
  }
297
308
  return s.nested("params").f(fieldName, coerceSchema(schema));
298
309
  }),
@@ -386,7 +397,7 @@ function params(route) {
386
397
  responses.push(builder);
387
398
  });
388
399
  if (responses.length === 0) {
389
- throw new Error("[rescript-rest] At least single response should be registered");
400
+ panic("At least single response should be registered", undefined);
390
401
  }
391
402
  params$2 = {
392
403
  method: definition.method,
@@ -444,7 +455,7 @@ function getCompletePath(baseUrl, pathItems, maybeQuery, maybeParams, jsonQueryO
444
455
  if (param !== undefined) {
445
456
  path = path + param;
446
457
  } else {
447
- throw new Error("[rescript-rest] " + ("Path parameter \"" + name + "\" is not defined in input"));
458
+ panic("Path parameter \"" + name + "\" is not defined in input", undefined);
448
459
  }
449
460
  }
450
461
  }
@@ -494,11 +505,7 @@ function $$fetch$1(route, input, client) {
494
505
  client$1 = client;
495
506
  } else {
496
507
  var client$2 = $$global.c;
497
- if (client$2 !== undefined) {
498
- client$1 = client$2;
499
- } else {
500
- throw new Error("[rescript-rest] " + ("Client is not set for the " + match.path + " fetch request. Please, use Rest.setGlobalClient or pass a client explicitly to the Rest.fetch arguments"));
501
- }
508
+ client$1 = client$2 !== undefined ? client$2 : panic("Client is not set for the " + match.path + " fetch request. Please, use Rest.setGlobalClient or pass a client explicitly to the Rest.fetch arguments", undefined);
502
509
  }
503
510
  var data = S$RescriptSchema.reverseConvertOrThrow(input, match.inputSchema);
504
511
  if (data.body !== (void 0)) {
@@ -528,11 +535,11 @@ function $$fetch$1(route, input, client) {
528
535
  var error$1 = error._1;
529
536
  var match = error$1.code;
530
537
  if (typeof match === "object" && match.TAG === "InvalidType" && error$1.path === S$RescriptSchema.Path.empty) {
531
- var message = "Failed parsing response data. Reason: Expected " + getSchemaField(match.expected, "data").schema.n() + ", received " + match.received.data;
532
- throw new Error("[rescript-rest] " + message);
538
+ return panic("Failed parsing response data. Reason: Expected " + getSchemaField(match.expected, "data").schema.n() + ", received " + match.received.data, undefined);
533
539
  }
534
- var message$1 = "Failed parsing response at " + error$1.path + ". Reason: " + S$RescriptSchema.$$Error.reason(error$1);
535
- throw new Error("[rescript-rest] " + message$1);
540
+ return panic("Failed parsing response at " + error$1.path + ". Reason: " + S$RescriptSchema.$$Error.reason(error$1), {
541
+ response: fetcherResponse
542
+ });
536
543
  }
537
544
  throw error;
538
545
  }
@@ -541,7 +548,7 @@ function $$fetch$1(route, input, client) {
541
548
  if (fetcherResponse.data && typeof fetcherResponse.data.message === "string") {
542
549
  error$2 = error$2 + ". Message: " + fetcherResponse.data.message;
543
550
  }
544
- throw new Error("[rescript-rest] " + error$2);
551
+ return panic(error$2, undefined);
545
552
  }
546
553
  });
547
554
  }
@@ -557,9 +564,11 @@ function client(baseUrl, fetcherOpt) {
557
564
  function setGlobalClient(baseUrl, fetcher) {
558
565
  var match = $$global.c;
559
566
  if (match !== undefined) {
560
- throw new Error("[rescript-rest] There's already a global client defined. You can have only one global client at a time.");
567
+ return panic("There's already a global client defined. You can have only one global client at a time.", undefined);
568
+ } else {
569
+ $$global.c = client(baseUrl, fetcher);
570
+ return ;
561
571
  }
562
- $$global.c = client(baseUrl, fetcher);
563
572
  }
564
573
 
565
574
  var $$Response = {};