| 1 |
# THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE. |
| 2 |
# Copyright (C) NIWA & British Crown (Met Office) & Contributors. |
| 3 |
# |
| 4 |
# This program is free software: you can redistribute it and/or modify |
| 5 |
# it under the terms of the GNU General Public License as published by |
| 6 |
# the Free Software Foundation, either version 3 of the License, or |
| 7 |
# (at your option) any later version. |
| 8 |
# |
| 9 |
# This program is distributed in the hope that it will be useful, |
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
# GNU General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU General Public License |
| 15 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
"""Exceptions for "expected" errors.""" |
| 17 |
|
| 18 |
|
| 19 |
from textwrap import wrap |
| 20 |
from typing import ( |
| 21 |
TYPE_CHECKING, |
| 22 |
Dict, |
| 23 |
Optional, |
| 24 |
Sequence, |
| 25 |
Set, |
| 26 |
Union, |
| 27 |
) |
| 28 |
|
| 29 |
from cylc.flow import __version__ as CYLC_VERSION |
| 30 |
from cylc.flow.util import format_cmd |
| 31 |
|
| 32 |
|
| 33 |
if TYPE_CHECKING: |
| 34 |
from cylc.flow.subprocctx import SubFuncContext |
| 35 |
|
| 36 |
|
| 37 |
class CylcError(Exception): |
| 38 |
"""Generic exception for Cylc errors. |
| 39 |
|
| 40 |
This exception is raised in-place of "expected" errors where a short |
| 41 |
message to the user is more appropriate than traceback. |
| 42 |
|
| 43 |
CLI commands will catch this exception and exit with str(exception). |
| 44 |
""" |
| 45 |
|
| 46 |
|
| 47 |
class PluginError(CylcError): |
| 48 |
"""Represents an error arising from a Cylc plugin. |
| 49 |
|
| 50 |
Args: |
| 51 |
entry_point: |
| 52 |
The plugin entry point as defined in setup.cfg |
| 53 |
(e.g. 'cylc.main_loop') |
| 54 |
plugin_name: |
| 55 |
Name of the plugin |
| 56 |
exc: |
| 57 |
Original exception caught when trying to run the plugin |
| 58 |
|
| 59 |
""" |
| 60 |
|
| 61 |
def __init__(self, entry_point: str, plugin_name: str, exc: Exception): |
| 62 |
self.entry_point = entry_point |
| 63 |
self.plugin_name = plugin_name |
| 64 |
self.exc = exc |
| 65 |
|
| 66 |
def __str__(self) -> str: |
| 67 |
return ( |
| 68 |
f"Error in plugin {self.entry_point}.{self.plugin_name}\n" |
| 69 |
f"{type(self.exc).__name__}: {self.exc}" |
| 70 |
) |
| 71 |
|
| 72 |
|
| 73 |
class InputError(CylcError): |
| 74 |
"""Exception covering erroneous user input to a Cylc interface. |
| 75 |
|
| 76 |
Ideally this would be handled in the interface (e.g. argument parser). |
| 77 |
If this isn't possible raise InputError. |
| 78 |
|
| 79 |
""" |
| 80 |
|
| 81 |
|
| 82 |
class CylcConfigError(CylcError): |
| 83 |
"""Generic exception to handle an error in a Cylc configuration file.""" |
| 84 |
# TODO: reference the configuration element causing the problem |
| 85 |
|
| 86 |
|
| 87 |
class WorkflowConfigError(CylcConfigError): |
| 88 |
"""Exception for configuration errors in a Cylc workflow configuration.""" |
| 89 |
|
| 90 |
|
| 91 |
class GlobalConfigError(CylcConfigError): |
| 92 |
"""Exception for configuration errors in a Cylc global configuration.""" |
| 93 |
|
| 94 |
|
| 95 |
class GraphParseError(WorkflowConfigError): |
| 96 |
"""Exception for errors in Cylc workflow graphing.""" |
| 97 |
|
| 98 |
|
| 99 |
class TriggerExpressionError(GraphParseError): |
| 100 |
"""Trigger expression syntax issue.""" |
| 101 |
|
| 102 |
|
| 103 |
class TaskProxySequenceBoundsError(CylcError): |
| 104 |
"""Error on TaskProxy.__init__ with out of sequence bounds start point.""" |
| 105 |
|
| 106 |
def __init__(self, msg): |
| 107 |
CylcError.__init__( |
| 108 |
self, 'Not loading %s (out of sequence bounds)' % msg) |
| 109 |
|
| 110 |
|
| 111 |
class ParamExpandError(WorkflowConfigError): |
| 112 |
"""Exception for errors in Cylc parameter expansion.""" |
| 113 |
|
| 114 |
|
| 115 |
class WorkflowEventError(CylcError): |
| 116 |
"""Exception for errors in Cylc event handlers.""" |
| 117 |
|
| 118 |
|
| 119 |
class CommandFailedError(CylcError): |
| 120 |
"""Exception for when scheduler commands fail.""" |
| 121 |
def __init__(self, value: Union[str, Exception]): |
| 122 |
self.value = value |
| 123 |
|
| 124 |
def __str__(self) -> str: |
| 125 |
if isinstance(self.value, Exception): |
| 126 |
return f"{type(self.value).__name__}: {self.value}" |
| 127 |
return self.value |
| 128 |
|
| 129 |
|
| 130 |
class ServiceFileError(CylcError): |
| 131 |
"""Exception for errors related to workflow service files.""" |
| 132 |
|
| 133 |
|
| 134 |
class WorkflowFilesError(CylcError): |
| 135 |
"""Exception for errors related to workflow files/directories.""" |
| 136 |
bullet = "\n -" |
| 137 |
|
| 138 |
|
| 139 |
class SchedulerAlive(CylcError): |
| 140 |
"""Workflow contact file exists and scheduler is alive.""" |
| 141 |
|
| 142 |
|
| 143 |
class FileRemovalError(CylcError): |
| 144 |
"""Exception for errors during deletion of files/directories, which are |
| 145 |
probably the filesystem's fault, not Cylc's.""" |
| 146 |
|
| 147 |
def __init__(self, exc: Exception) -> None: |
| 148 |
CylcError.__init__( |
| 149 |
self, |
| 150 |
f"{exc}. This is probably a temporary issue with the filesystem, " |
| 151 |
"not a problem with Cylc." |
| 152 |
) |
| 153 |
|
| 154 |
|
| 155 |
class PlatformError(CylcError): |
| 156 |
"""Error in the management of a remote platform. |
| 157 |
|
| 158 |
If the exception represents a command failure, provide either the ctx OR |
| 159 |
manually populate the remaining kwargs. Otherwise leave the kwargs blank. |
| 160 |
|
| 161 |
Args: |
| 162 |
message: |
| 163 |
Short description. |
| 164 |
platform_name: |
| 165 |
Name of the platform. |
| 166 |
ctx: |
| 167 |
SubFuncContext object if available. |
| 168 |
The other kwargs are derived from this. |
| 169 |
cmd: |
| 170 |
The remote command. |
| 171 |
ret_code: |
| 172 |
The command's return code. |
| 173 |
out: |
| 174 |
The command's stdout. |
| 175 |
err: |
| 176 |
The command's stderr. |
| 177 |
|
| 178 |
""" |
| 179 |
|
| 180 |
MSG_INIT = "initialisation did not complete" |
| 181 |
MSG_SELECT = "host selection failed" |
| 182 |
MSG_TIDY = "clean up did not complete" |
| 183 |
|
| 184 |
def __init__( |
| 185 |
self, |
| 186 |
message: str, |
| 187 |
platform_name: str | None, |
| 188 |
*, |
| 189 |
ctx: 'SubFuncContext | None' = None, |
| 190 |
cmd: str | Sequence[str] | None = None, |
| 191 |
ret_code: int | None = None, |
| 192 |
out: str | None = None, |
| 193 |
err: str | None = None |
| 194 |
) -> None: |
| 195 |
self.msg = message |
| 196 |
self.platform_name = platform_name |
| 197 |
if ctx: |
| 198 |
self.cmd = ctx.cmd |
| 199 |
self.ret_code = ctx.ret_code |
| 200 |
self.out = ctx.out |
| 201 |
self.err = ctx.err |
| 202 |
else: |
| 203 |
self.cmd = cmd |
| 204 |
self.ret_code = ret_code |
| 205 |
self.out = out |
| 206 |
self.err = err |
| 207 |
# convert the cmd object to a str if needed |
| 208 |
if self.cmd and not isinstance(self.cmd, str): |
| 209 |
self.cmd = format_cmd(self.cmd) |
| 210 |
|
| 211 |
def __str__(self): |
| 212 |
# matches cylc.flow.platforms.log_platform_event format |
| 213 |
if self.platform_name: |
| 214 |
ret = f'platform: {self.platform_name} - {self.msg}' |
| 215 |
else: |
| 216 |
ret = f'{self.msg}' |
| 217 |
for label, item in [ |
| 218 |
('COMMAND', self.cmd), |
| 219 |
('RETURN CODE', self.ret_code), |
| 220 |
('STDOUT', self.out), |
| 221 |
('STDERR', self.err) |
| 222 |
]: |
| 223 |
if item is not None: |
| 224 |
ret += f'\n{label}:' |
| 225 |
for line in str(item).splitlines(True): # keep newline chars |
| 226 |
ret += f"\n {line}" |
| 227 |
return ret |
| 228 |
|
| 229 |
|
| 230 |
class TaskDefError(WorkflowConfigError): |
| 231 |
"""Exception raise for errors in TaskDef initialization.""" |
| 232 |
|
| 233 |
|
| 234 |
class XtriggerConfigError(WorkflowConfigError): |
| 235 |
"""An error in an xtrigger. |
| 236 |
|
| 237 |
For example: |
| 238 |
|
| 239 |
* If the function module was not found. |
| 240 |
* If the function was not found in the xtrigger module. |
| 241 |
* If the function is not callable. |
| 242 |
* If any string template in the function context |
| 243 |
arguments are not present in the expected template values. |
| 244 |
|
| 245 |
""" |
| 246 |
|
| 247 |
def __init__(self, label: str, func: str, message: Union[str, Exception]): |
| 248 |
self.label = label |
| 249 |
self.func = func |
| 250 |
self.message = message |
| 251 |
|
| 252 |
def __str__(self) -> str: |
| 253 |
return f'[@{self.label}] {self.func}\n{self.message}' |
| 254 |
|
| 255 |
|
| 256 |
class ClientError(CylcError): |
| 257 |
"""Base class for errors raised by Cylc client instances. |
| 258 |
|
| 259 |
For example, the workflow you are trying to connect to is stopped. |
| 260 |
|
| 261 |
Attributes: |
| 262 |
message: |
| 263 |
The exception message. |
| 264 |
traceback: |
| 265 |
The underlying exception instance if available. |
| 266 |
workflow: |
| 267 |
The workflow ID if available. |
| 268 |
""" |
| 269 |
|
| 270 |
def __init__( |
| 271 |
self, |
| 272 |
message: str, |
| 273 |
traceback: Optional[str] = None, |
| 274 |
workflow: Optional[str] = None |
| 275 |
): |
| 276 |
self.message = message |
| 277 |
self.traceback = traceback |
| 278 |
# Workflow not included in string representation but useful bit of |
| 279 |
# info to attach to the exception object |
| 280 |
self.workflow = workflow |
| 281 |
|
| 282 |
def __str__(self) -> str: |
| 283 |
ret = self.message |
| 284 |
if self.traceback: |
| 285 |
# append server-side traceback |
| 286 |
ret += '\n' + self.traceback |
| 287 |
return ret |
| 288 |
|
| 289 |
|
| 290 |
class RequestError(ClientError): |
| 291 |
"""Represents an error handling a request, returned by the server.""" |
| 292 |
|
| 293 |
def __init__( |
| 294 |
self, message: str, workflow_cylc_version: Optional[str] = None |
| 295 |
): |
| 296 |
ClientError.__init__( |
| 297 |
self, |
| 298 |
message, |
| 299 |
traceback=( |
| 300 |
f"(Workflow is running in Cylc {workflow_cylc_version})" |
| 301 |
if workflow_cylc_version |
| 302 |
and workflow_cylc_version != CYLC_VERSION |
| 303 |
else None |
| 304 |
), |
| 305 |
) |
| 306 |
|
| 307 |
|
| 308 |
class WorkflowStopped(ClientError): |
| 309 |
"""The Cylc scheduler you attempted to connect to is stopped.""" |
| 310 |
|
| 311 |
def __init__(self, workflow): |
| 312 |
self.workflow = workflow |
| 313 |
|
| 314 |
def __str__(self): |
| 315 |
return f'{self.workflow} is not running' |
| 316 |
|
| 317 |
|
| 318 |
class ClientTimeout(CylcError): |
| 319 |
"""The scheduler did not respond within the timeout. |
| 320 |
|
| 321 |
This could be due to: |
| 322 |
* Network issues. |
| 323 |
* Scheduler issues. |
| 324 |
* Insufficient timeout. |
| 325 |
|
| 326 |
To increase the timeout, use the ``--comms-timeout`` option. |
| 327 |
|
| 328 |
""" |
| 329 |
|
| 330 |
def __init__(self, message: str, workflow: Optional[str] = None): |
| 331 |
self.message = message |
| 332 |
self.workflow = workflow |
| 333 |
|
| 334 |
def __str__(self) -> str: |
| 335 |
return self.message |
| 336 |
|
| 337 |
|
| 338 |
class CyclingError(CylcError): |
| 339 |
"""Base class for errors in cycling configuration.""" |
| 340 |
|
| 341 |
|
| 342 |
class CyclerTypeError(CyclingError): |
| 343 |
"""An error raised when incompatible cycling types are wrongly mixed.""" |
| 344 |
|
| 345 |
def __init__(self, *args): |
| 346 |
CyclingError.__init__( |
| 347 |
self, |
| 348 |
'Incompatible cycling types: {0} ({1}), {2} ({3})'.format(*args)) |
| 349 |
|
| 350 |
|
| 351 |
class PointParsingError(CyclingError): |
| 352 |
"""An error raised when a point has an incorrect value.""" |
| 353 |
|
| 354 |
def __init__(self, *args): |
| 355 |
CyclingError.__init__( |
| 356 |
self, 'Incompatible value for {0}: {1}: {2}'.format(*args)) |
| 357 |
|
| 358 |
|
| 359 |
class IntervalParsingError(CyclingError): |
| 360 |
"""An error raised when an interval has an incorrect value.""" |
| 361 |
|
| 362 |
def __init__(self, *args): |
| 363 |
CyclingError.__init__( |
| 364 |
self, 'Incompatible value for {0}: {1}'.format(*args)) |
| 365 |
|
| 366 |
|
| 367 |
class SequenceParsingError(CyclingError): |
| 368 |
"""Error on parsing an invalid sequence.""" |
| 369 |
|
| 370 |
|
| 371 |
class SequenceDegenerateError(CyclingError): |
| 372 |
"""An error raised when adjacent points on a sequence are equal.""" |
| 373 |
|
| 374 |
def __init__(self, *args): |
| 375 |
CyclingError.__init__( |
| 376 |
self, ( |
| 377 |
'{0}, point format {1}: equal adjacent points:' |
| 378 |
' {2} => {3}.' |
| 379 |
).format(*args)) |
| 380 |
|
| 381 |
|
| 382 |
class CylcTimeSyntaxError(CyclingError): |
| 383 |
"""An error denoting invalid ISO/Cylc input syntax.""" |
| 384 |
|
| 385 |
|
| 386 |
class CylcMissingContextPointError(CyclingError): |
| 387 |
"""An error denoting a missing (but required) context cycle point.""" |
| 388 |
|
| 389 |
|
| 390 |
class CylcMissingFinalCyclePointError(CyclingError): |
| 391 |
"""An error denoting a missing (but required) final cycle point.""" |
| 392 |
|
| 393 |
|
| 394 |
class PlatformLookupError(CylcConfigError): |
| 395 |
"""Unable to determine the correct job platform from the information |
| 396 |
given""" |
| 397 |
|
| 398 |
|
| 399 |
class HostSelectException(CylcError): |
| 400 |
"""No hosts could be selected from the provided configuration. |
| 401 |
|
| 402 |
Args: |
| 403 |
data: Mapping of hostnames to error info. |
| 404 |
ranking: The ranking expression used to select the host. |
| 405 |
""" |
| 406 |
|
| 407 |
def __init__(self, data: Dict[str, dict], ranking: Optional[str] = None): |
| 408 |
self.data = data |
| 409 |
self.ranking = ranking |
| 410 |
|
| 411 |
def __str__(self) -> str: |
| 412 |
ret = 'Could not select host from:' |
| 413 |
for host, data in sorted(self.data.items()): |
| 414 |
ret += f'\n {host}:' |
| 415 |
for key, value in data.items(): |
| 416 |
ret += f'\n {key}: {value}' |
| 417 |
hint = self.get_hint() |
| 418 |
if hint: |
| 419 |
ret += f'\n\n{hint}' |
| 420 |
return ret |
| 421 |
|
| 422 |
def get_hint(self) -> Optional[str]: |
| 423 |
"""Return a hint to explain this error for certain cases.""" |
| 424 |
if all( |
| 425 |
# all procs came back with special SSH error code 255 |
| 426 |
datum.get('returncode') == 255 |
| 427 |
for datum in self.data.values() |
| 428 |
): |
| 429 |
# likely SSH issues |
| 430 |
return ( |
| 431 |
'Cylc could not establish SSH connection to the run hosts.' |
| 432 |
'\nEnsure you can SSH to these hosts without having to' |
| 433 |
' answer any prompts.' |
| 434 |
) |
| 435 |
|
| 436 |
if ( |
| 437 |
# a ranking expression was used |
| 438 |
self.ranking |
| 439 |
# and all procs came back with special 'cylc psutil' error code 2 |
| 440 |
# (which is used for errors relating to the extraction of metrics) |
| 441 |
and all( |
| 442 |
datum.get('returncode') == 2 |
| 443 |
for datum in self.data.values() |
| 444 |
) |
| 445 |
): |
| 446 |
# likely an issue with the ranking expression |
| 447 |
lines = wrap( |
| 448 |
self.ranking, |
| 449 |
initial_indent=' ', |
| 450 |
subsequent_indent=' ', |
| 451 |
) |
| 452 |
ranking = "\n".join(lines) |
| 453 |
return ( |
| 454 |
'This is likely an error in the ranking expression:' |
| 455 |
f'\n{ranking}' |
| 456 |
'\n\nConfigured by:' |
| 457 |
'\n global.cylc[scheduler][run hosts]ranking' |
| 458 |
) |
| 459 |
|
| 460 |
return None |
| 461 |
|
| 462 |
|
| 463 |
class NoHostsError(CylcError): |
| 464 |
"""None of the hosts of a given platform were reachable.""" |
| 465 |
def __init__(self, platform): |
| 466 |
self.platform_name = platform['name'] |
| 467 |
|
| 468 |
def __str__(self): |
| 469 |
return f'Unable to find valid host for {self.platform_name}' |
| 470 |
|
| 471 |
|
| 472 |
class NoPlatformsError(PlatformLookupError): |
| 473 |
"""None of the platforms of a given set were reachable. |
| 474 |
|
| 475 |
Args: |
| 476 |
identity: |
| 477 |
The name of the platform group or install target. |
| 478 |
set_type: |
| 479 |
Whether the set of platforms is a platform group or an install |
| 480 |
target. |
| 481 |
place: |
| 482 |
Where the attempt to get the platform failed. |
| 483 |
""" |
| 484 |
def __init__( |
| 485 |
self, |
| 486 |
identity: str, |
| 487 |
hosts_consumed: Set[str], |
| 488 |
set_type: str = 'group', |
| 489 |
place: str = '', |
| 490 |
): |
| 491 |
self.identity = identity |
| 492 |
self.type = set_type |
| 493 |
self.hosts_consumed = hosts_consumed |
| 494 |
if place: |
| 495 |
self.place = f' during {place}.' |
| 496 |
else: |
| 497 |
self.place = '.' |
| 498 |
|
| 499 |
def __str__(self): |
| 500 |
return ( |
| 501 |
f'Unable to find a platform from {self.type} {self.identity}' |
| 502 |
f'{self.place}' |
| 503 |
) |
| 504 |
|
| 505 |
|
| 506 |
class CylcVersionError(CylcError): |
| 507 |
"""Contact file is for a Cylc Version not supported by this script.""" |
| 508 |
def __init__(self, version=None): |
| 509 |
self.version = version |
| 510 |
|
| 511 |
def __str__(self): |
| 512 |
if self.version is not None: |
| 513 |
return ( |
| 514 |
f'Installed Cylc {self.version} workflow is not ' |
| 515 |
'compatible with Cylc 8.' |
| 516 |
) |
| 517 |
else: |
| 518 |
return "Installed workflow is not compatible with Cylc 8." |
| 519 |
|
| 520 |
|
| 521 |
class InvalidCompletionExpression(CylcError): |
| 522 |
"""For the [runtime][<namespace>]completion configuration. |
| 523 |
|
| 524 |
Raised when non-whitelisted syntax is present. |
| 525 |
""" |
| 526 |
def __init__(self, message, expr=None): |
| 527 |
self.message = message |
| 528 |
self.expr = expr |
| 529 |
|
| 530 |
def __str__(self): |
| 531 |
return self.message |