UNPKG

@temporalio/worker

Version:
132 lines 6.25 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.decodePayload = decodePayload; exports.operationErrorToProto = operationErrorToProto; exports.handlerErrorToProto = handlerErrorToProto; exports.coerceToHandlerError = coerceToHandlerError; const grpc_js_1 = require("@grpc/grpc-js"); const nexus = __importStar(require("nexus-rpc")); const client_1 = require("@temporalio/client"); const common_1 = require("@temporalio/common"); const internal_non_workflow_1 = require("@temporalio/common/lib/internal-non-workflow"); //////////////////////////////////////////////////////////////////////////////////////////////////// // Payloads //////////////////////////////////////////////////////////////////////////////////////////////////// async function decodePayload(dataConverter, payload) { let decoded; try { decoded = await (0, internal_non_workflow_1.decodeOptionalSingle)(dataConverter.payloadCodecs, payload); } catch (err) { if (err instanceof common_1.ApplicationFailure) { throw err; } throw new nexus.HandlerError('INTERNAL', `Payload codec failed to decode Nexus operation input`, { cause: err }); } if (decoded == null) { return undefined; } try { return dataConverter.payloadConverter.fromPayload(decoded); } catch (err) { if (err instanceof common_1.ApplicationFailure) { throw err; } throw new nexus.HandlerError('BAD_REQUEST', `Payload converter failed to decode Nexus operation input`, { cause: err, }); } } //////////////////////////////////////////////////////////////////////////////////////////////////// // Failures //////////////////////////////////////////////////////////////////////////////////////////////////// async function operationErrorToProto(dataConverter, err) { let newError; if (err.state === 'canceled') { newError = new common_1.CancelledFailure(err.message, undefined, err.cause); } else { newError = common_1.ApplicationFailure.create({ message: err.message, type: 'OperationError', nonRetryable: true, cause: err.cause, }); } newError.stack = err.stack; return await (0, internal_non_workflow_1.encodeErrorToFailure)(dataConverter, newError); } async function handlerErrorToProto(dataConverter, err) { return await (0, internal_non_workflow_1.encodeErrorToFailure)(dataConverter, err); } function coerceToHandlerError(err) { if (err instanceof nexus.HandlerError) { return err; } // REVIEW: This check could be moved down and fold into the next one but will keep for now to help readability. if (err instanceof common_1.ApplicationFailure && err.nonRetryable) { return new nexus.HandlerError('INTERNAL', 'Handler failed with non-retryable application error', { cause: err, retryableOverride: false, }); } if (err instanceof client_1.ServiceError) { if ((0, client_1.isGrpcServiceError)(err.cause)) { switch (err.cause.code) { case grpc_js_1.status.INVALID_ARGUMENT: return new nexus.HandlerError('BAD_REQUEST', undefined, { cause: err }); case grpc_js_1.status.ALREADY_EXISTS: case grpc_js_1.status.FAILED_PRECONDITION: case grpc_js_1.status.OUT_OF_RANGE: return new nexus.HandlerError('INTERNAL', undefined, { cause: err, retryableOverride: false }); case grpc_js_1.status.ABORTED: case grpc_js_1.status.UNAVAILABLE: return new nexus.HandlerError('UNAVAILABLE', undefined, { cause: err }); case grpc_js_1.status.CANCELLED: case grpc_js_1.status.DATA_LOSS: case grpc_js_1.status.INTERNAL: case grpc_js_1.status.UNKNOWN: case grpc_js_1.status.UNAUTHENTICATED: case grpc_js_1.status.PERMISSION_DENIED: // Note that UNAUTHENTICATED and PERMISSION_DENIED have Nexus error types but we convert to internal because // this is not a client auth error and happens when the handler fails to auth with Temporal and should be // considered retryable. return new nexus.HandlerError('INTERNAL', undefined, { cause: err }); case grpc_js_1.status.NOT_FOUND: return new nexus.HandlerError('NOT_FOUND', undefined, { cause: err }); case grpc_js_1.status.RESOURCE_EXHAUSTED: return new nexus.HandlerError('RESOURCE_EXHAUSTED', undefined, { cause: err }); case grpc_js_1.status.UNIMPLEMENTED: return new nexus.HandlerError('NOT_IMPLEMENTED', undefined, { cause: err }); case grpc_js_1.status.DEADLINE_EXCEEDED: return new nexus.HandlerError('UPSTREAM_TIMEOUT', undefined, { cause: err }); } } } return new nexus.HandlerError('INTERNAL', undefined, { cause: err }); } //# sourceMappingURL=conversions.js.map