Skip to content

Commit adbf567

Browse files
authored
Merge pull request #10 from Innmind/more-explicit-errors
Use `Attempt` to better explicit everywhere an action may fail
2 parents dcb9336 + e80ce0b commit adbf567

26 files changed

Lines changed: 127 additions & 103 deletions

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,15 @@
1414
- Requires `innmind/io:~3.2`
1515
- Requires `innmind/immutable:~5.15`
1616
- `Innmind\OperatingSystem\Config::withHttpHeartbeat()` period is now expressed with a `Innmind\TimeContinuum\Period`
17+
- `Innmind\OperatingSystem\CurrentProcess::id()` now returns an `Innmind\Immutable\Attempt`
1718
- `Innmind\OperatingSystem\CurrentProcess::halt()` now returns `Innmind\Immutable\Attempt<Innmind\Immutable\SideEffect>`
19+
- `Innmind\OperatingSystem\Filesystem::mount()` now returns an `Innmind\Immutable\Attempt`
20+
- `Innmind\OperatingSystem\Filesystem::temporary()` now returns an `Innmind\Immutable\Attempt`
21+
- `Innmind\OperatingSystem\Ports::open()` now returns an `Innmind\Immutable\Attempt`
22+
- `Innmind\OperatingSystem\Remote::socket()` now returns an `Innmind\Immutable\Attempt`
23+
- `Innmind\OperatingSystem\Sockets::open()` now returns an `Innmind\Immutable\Attempt`
24+
- `Innmind\OperatingSystem\Sockets::takeOver()` now returns an `Innmind\Immutable\Attempt`
25+
- `Innmind\OperatingSystem\Sockets::connectTo()` now returns an `Innmind\Immutable\Attempt`
1826

1927
### Fixed
2028

src/CurrentProcess.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@
1414

1515
interface CurrentProcess
1616
{
17-
public function id(): Pid;
17+
/**
18+
* @return Attempt<Pid>
19+
*/
20+
public function id(): Attempt;
1821
public function signals(): Signals;
1922

2023
/**

src/CurrentProcess/Generic.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@ public static function of(Halt $halt): self
3030
}
3131

3232
#[\Override]
33-
public function id(): Pid
33+
public function id(): Attempt
3434
{
35-
/**
36-
* @psalm-suppress ArgumentTypeCoercion
37-
* @psalm-suppress PossiblyFalseArgument
38-
*/
39-
return new Pid(\getmypid());
35+
$pid = \getmypid();
36+
37+
/** @psalm-suppress ArgumentTypeCoercion */
38+
return match ($pid) {
39+
false => Attempt::error(new \RuntimeException('Failed to retrieve process id')),
40+
default => Attempt::result(new Pid($pid)),
41+
};
4042
}
4143

4244
#[\Override]

src/CurrentProcess/Logger.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
namespace Innmind\OperatingSystem\CurrentProcess;
55

66
use Innmind\OperatingSystem\CurrentProcess;
7-
use Innmind\Server\Control\Server\Process\Pid;
87
use Innmind\Server\Status\Server\Memory\Bytes;
98
use Innmind\TimeContinuum\Period;
109
use Innmind\Immutable\Attempt;
@@ -28,16 +27,16 @@ public static function psr(CurrentProcess $process, LoggerInterface $logger): se
2827
}
2928

3029
#[\Override]
31-
public function id(): Pid
30+
public function id(): Attempt
3231
{
33-
$pid = $this->process->id();
32+
return $this->process->id()->map(function($pid) {
33+
$this->logger->debug(
34+
'Current process id is {pid}',
35+
['pid' => $pid->toInt()],
36+
);
3437

35-
$this->logger->debug(
36-
'Current process id is {pid}',
37-
['pid' => $pid->toInt()],
38-
);
39-
40-
return $pid;
38+
return $pid;
39+
});
4140
}
4241

4342
#[\Override]

src/Filesystem.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,18 @@
1010
use Innmind\Url\Path;
1111
use Innmind\FileWatch\Ping;
1212
use Innmind\Immutable\{
13+
Attempt,
1314
Maybe,
1415
Str,
1516
Sequence,
1617
};
1718

1819
interface Filesystem
1920
{
20-
public function mount(Path $path): Adapter;
21+
/**
22+
* @return Attempt<Adapter>
23+
*/
24+
public function mount(Path $path): Attempt;
2125
public function contains(Path $path): bool;
2226

2327
/**
@@ -36,7 +40,7 @@ public function watch(Path $path): Ping;
3640
*
3741
* @param Sequence<Maybe<Str>> $chunks
3842
*
39-
* @return Maybe<Content>
43+
* @return Attempt<Content>
4044
*/
41-
public function temporary(Sequence $chunks): Maybe;
45+
public function temporary(Sequence $chunks): Attempt;
4246
}

src/Filesystem/Generic.php

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,28 +48,30 @@ public static function of(Processes $processes, Config $config): self
4848
}
4949

5050
#[\Override]
51-
public function mount(Path $path): Adapter
51+
public function mount(Path $path): Attempt
5252
{
5353
/**
5454
* @var Adapter $adapter
5555
* @var string $mounted
5656
*/
5757
foreach ($this->mounted as $adapter => $mounted) {
5858
if ($path->toString() === $mounted) {
59-
return $adapter;
59+
return Attempt::result($adapter);
6060
}
6161
}
6262

63-
$adapter = Adapter\Filesystem::mount(
64-
$path,
65-
$this->config->io(),
66-
)
67-
->withCaseSensitivity(
68-
$this->config->filesystemCaseSensitivity(),
69-
);
70-
$this->mounted[$adapter] = $path->toString();
71-
72-
return $adapter;
63+
return Attempt::of(function() use ($path) {
64+
$adapter = Adapter\Filesystem::mount(
65+
$path,
66+
$this->config->io(),
67+
)
68+
->withCaseSensitivity(
69+
$this->config->filesystemCaseSensitivity(),
70+
);
71+
$this->mounted[$adapter] = $path->toString();
72+
73+
return $adapter;
74+
});
7375
}
7476

7577
#[\Override]
@@ -111,7 +113,7 @@ public function watch(Path $path): Ping
111113
}
112114

113115
#[\Override]
114-
public function temporary(Sequence $chunks): Maybe
116+
public function temporary(Sequence $chunks): Attempt
115117
{
116118
return Attempt::of(
117119
fn() => $this
@@ -129,6 +131,6 @@ public function temporary(Sequence $chunks): Maybe
129131
->map(static fn($tmp) => $tmp->read())
130132
->map(Content::io(...))
131133
->unwrap(),
132-
)->maybe();
134+
);
133135
}
134136
}

src/Filesystem/Logger.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Innmind\Url\Path;
1212
use Innmind\FileWatch\Ping;
1313
use Innmind\Immutable\{
14+
Attempt,
1415
Maybe,
1516
Sequence,
1617
};
@@ -37,11 +38,13 @@ public static function psr(
3738
}
3839

3940
#[\Override]
40-
public function mount(Path $path): Adapter
41+
public function mount(Path $path): Attempt
4142
{
42-
return Adapter\Logger::psr(
43-
$this->filesystem->mount($path),
44-
$this->logger,
43+
return $this->filesystem->mount($path)->map(
44+
fn($adapter) => Adapter\Logger::psr(
45+
$adapter,
46+
$this->logger,
47+
),
4548
);
4649
}
4750

@@ -82,7 +85,7 @@ public function watch(Path $path): Ping
8285
}
8386

8487
#[\Override]
85-
public function temporary(Sequence $chunks): Maybe
88+
public function temporary(Sequence $chunks): Attempt
8689
{
8790
return $this
8891
->filesystem

src/Ports.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
Sockets\Internet\Transport,
1010
};
1111
use Innmind\IP\IP;
12-
use Innmind\Immutable\Maybe;
12+
use Innmind\Immutable\Attempt;
1313

1414
interface Ports
1515
{
1616
/**
17-
* @return Maybe<Server>
17+
* @return Attempt<Server>
1818
*/
19-
public function open(Transport $transport, IP $ip, Port $port): Maybe;
19+
public function open(Transport $transport, IP $ip, Port $port): Attempt;
2020
}

src/Ports/Logger.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use Innmind\Url\Authority\Port;
88
use Innmind\IO\Sockets\Internet\Transport;
99
use Innmind\IP\IP;
10-
use Innmind\Immutable\Maybe;
10+
use Innmind\Immutable\Attempt;
1111
use Psr\Log\LoggerInterface;
1212

1313
final class Logger implements Ports
@@ -27,7 +27,7 @@ public static function psr(Ports $ports, LoggerInterface $logger): self
2727
}
2828

2929
#[\Override]
30-
public function open(Transport $transport, IP $ip, Port $port): Maybe
30+
public function open(Transport $transport, IP $ip, Port $port): Attempt
3131
{
3232
$this->logger->debug(
3333
'Opening new port at {address}',

src/Ports/Unix.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use Innmind\Url\Authority\Port;
1111
use Innmind\IO\Sockets\Internet\Transport;
1212
use Innmind\IP\IP;
13-
use Innmind\Immutable\Maybe;
13+
use Innmind\Immutable\Attempt;
1414

1515
final class Unix implements Ports
1616
{
@@ -30,14 +30,13 @@ public static function of(Config $config): self
3030
}
3131

3232
#[\Override]
33-
public function open(Transport $transport, IP $ip, Port $port): Maybe
33+
public function open(Transport $transport, IP $ip, Port $port): Attempt
3434
{
3535
return $this
3636
->config
3737
->io()
3838
->sockets()
3939
->servers()
40-
->internet($transport, $ip, $port)
41-
->maybe();
40+
->internet($transport, $ip, $port);
4241
}
4342
}

0 commit comments

Comments
 (0)