Bug 37893: (QA follow-up): Check if port already in use when storing a listener

To test:
1) Ensure a port is already in use in the server
2) Try creating a new listener using that same port.
3) Notice the error 'Something went wrong: Error: Can't listen on port 127.0.0.1:8023/tcp/IPv4. Port already in use.' is shown on the UI

Co-authored-by: Martin Renvoize <martin.renvoize@openfifth.co.uk>
Signed-off-by: Andrew Fuerste Henry <andrew@bywatersolutions.com>
Signed-off-by: Lisette Scheer <lisette@bywatersolutions.com>
Signed-off-by: Martin Renvoize <martin.renvoize@openfifth.co.uk>
Signed-off-by: Lucas Gass <lucas@bywatersolutions.com>
This commit is contained in:
Pedro Amorim 2025-05-09 13:16:22 +00:00 committed by Lucas Gass
parent ee62bd459e
commit 2ed892b38c
No known key found for this signature in database
GPG Key ID: 60B447398B0D36CA
2 changed files with 36 additions and 0 deletions

View File

@ -108,6 +108,12 @@ sub add {
openapi => { error => "Given " . $to_api_mapping->{ $_->parameter } . " does not exist" }
);
}
elsif ( $_->isa('Koha::Exceptions::ObjectNotCreated') ) {
return $c->render(
status => 500,
openapi => { error => $_->error }
);
}
}
$c->unhandled_exception($_);

View File

@ -19,6 +19,8 @@ use Modern::Perl;
use base qw(Koha::Object);
use IO::Socket;
=head1 NAME
Koha::SIP2::Listener- Koha Sip Listener Object class
@ -29,6 +31,34 @@ Koha::SIP2::Listener- Koha Sip Listener Object class
=cut
=head3 store
store
=cut
sub store {
my ($self) = @_;
my ($port) = $self->port =~ /:(\d+)/;
my ($addr) = $self->port =~ /([0-9.]+)/;
my $socket = IO::Socket::INET->new(
LocalAddr => $addr,
LocalPort => $port,
Proto => 'tcp',
ReuseAddr => 0,
Listen => 1
);
unless ($socket) {
Koha::Exceptions::ObjectNotCreated->throw(
error => "Can't listen on port " . $self->port . ". Port already in use." );
}
return $self->SUPER::store;
}
=head3 get_for_config
Returns the listener hashref as expected by C4/SIP/Sip/Configuration->new;