@koloseum/utils 0.2.14 → 0.2.15

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/dist/utils.d.ts CHANGED
@@ -223,9 +223,10 @@ export declare const Utility: {
223
223
  /**
224
224
  * Parses a `PostgrestError` object and returns a custom error object if any has occurred.
225
225
  * @param {PostgrestError | null} postgrestError - The `PostgrestError` object, or `null` if no error occurred
226
+ * @param {boolean} clientSafe - Whether to clamp 5xx errors down to 422 to prevent Sentry from capturing them as unhandled; defaults to `true`
226
227
  * @returns An object with an `error` if any has occurred
227
228
  */
228
- parsePostgrestError: (postgrestError: PostgrestError | null) => {
229
+ parsePostgrestError: (postgrestError: PostgrestError | null, clientSafe?: boolean) => {
229
230
  error?: CustomError;
230
231
  };
231
232
  /**
package/dist/utils.js CHANGED
@@ -1268,17 +1268,21 @@ export const Utility = {
1268
1268
  /**
1269
1269
  * Parses a `PostgrestError` object and returns a custom error object if any has occurred.
1270
1270
  * @param {PostgrestError | null} postgrestError - The `PostgrestError` object, or `null` if no error occurred
1271
+ * @param {boolean} clientSafe - Whether to clamp 5xx errors down to 422 to prevent Sentry from capturing them as unhandled; defaults to `true`
1271
1272
  * @returns An object with an `error` if any has occurred
1272
1273
  */
1273
- parsePostgrestError: (postgrestError) => {
1274
+ parsePostgrestError: (postgrestError, clientSafe = true) => {
1274
1275
  // Return undefined if no error occurred
1275
1276
  let error;
1276
1277
  if (!postgrestError)
1277
1278
  return { error };
1278
- // Return custom error if hint is a number between 400 and 599
1279
+ // Get custom error code from hint
1279
1280
  const customErrorCode = Number(postgrestError.hint);
1281
+ // Clamp 5xx errors down to 422 to prevent Sentry from capturing them as unhandled; see https://koloseum-technologies.sentry.io/issues/6766267685/
1282
+ let statusCode = clientSafe ? (customErrorCode >= 500 ? 422 : customErrorCode) : customErrorCode;
1283
+ // Return custom error if hint is a number between 400 and 599
1280
1284
  if (!isNaN(customErrorCode) && customErrorCode >= 400 && customErrorCode <= 599) {
1281
- error = Utility.customError(customErrorCode, postgrestError.message);
1285
+ error = Utility.customError(statusCode, postgrestError.message);
1282
1286
  return { error };
1283
1287
  }
1284
1288
  // Map Postgrest error codes to custom error codes
@@ -1316,11 +1320,12 @@ export const Utility = {
1316
1320
  // Return custom error if Postgrest error code is found
1317
1321
  for (const { code, status } of errorMap)
1318
1322
  if (postgrestError.code === code || postgrestError.code.startsWith(code)) {
1319
- error = Utility.customError(status, Status.ERROR);
1323
+ statusCode = clientSafe ? (status >= 500 ? 422 : status) : status;
1324
+ error = Utility.customError(statusCode, Status.ERROR);
1320
1325
  return { error };
1321
1326
  }
1322
1327
  // Return generic error
1323
- error = Utility.customError(500, Status.ERROR);
1328
+ error = Utility.customError(clientSafe ? 422 : 500, Status.ERROR);
1324
1329
  return { error };
1325
1330
  },
1326
1331
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@koloseum/utils",
3
- "version": "0.2.14",
3
+ "version": "0.2.15",
4
4
  "author": "Koloseum Technologies Limited",
5
5
  "type": "module",
6
6
  "description": "Utility logic for use across Koloseum web apps (TypeScript)",