Handle errors#
Every error derives from modsmith.errors.ModsmithError, so one except can catch them all:
import modsmith as ms
from modsmith.errors import ModsmithError
try:
pack = ms.packs.load("pack.mrpack")
instance = ms.instances.Instance.open("instances/my-pack")
instance.sync(pack)
except ModsmithError as error:
print(f"Install failed: {error}")
Errors from your own mistakes, such as passing the wrong type, are ordinary Python errors.
Which error to catch#
Error |
Meaning |
|---|---|
|
A |
|
A request failed. |
|
A download failed. |
|
A file has the wrong size or hash |
|
The version, loader or format is not supported |
|
A modpack file is invalid |
|
A platform could not provide what was requested |
|
A loader could not be installed |
|
A Forge or NeoForge install step failed |
|
A Java runtime could not be found or installed |
|
The launcher’s files could not be used |
|
An instance could not be read or changed |
|
A pack still has files with no download |
ProcessorError is a kind of LoaderError, and UnresolvedEntriesError is a kind of InstanceError.
Report failed downloads#
A download keeps going when one file fails, then reports them all together:
from modsmith.errors import DownloadError
try:
instance.sync(pack)
except DownloadError as error:
for failure in error.failures:
print(failure)
Tell a bad key from a bad connection#
from modsmith.errors import NetworkError
try:
ms.platforms.Modrinth("my-tool/1.0").get_project("sodium")
except NetworkError as error:
if error.status is None:
print("No connection")
else:
print("The server answered", error.status)
Retries#
Modsmith already retries connection failures, timeouts, and HTTP 429 and 5xx responses, and waits as long as a server asks, up to one minute. A modsmith.net.Session lets you change this:
session = ms.net.Session("my-tool/1.0", attempts=6, backoff=2.0)
modrinth = ms.platforms.Modrinth(session=session)
Logs#
Modsmith writes warnings to the modsmith logger, such as when a download moves to another mirror. It stays silent unless you turn logging on:
import logging
logging.basicConfig(level=logging.INFO)